Embracing User Defined Literals Safely for Types that Behave as though Built-in
summary
Shows exactly how to write and choose among cooked, raw, template and string UDLs, using real code: a base‑3 numeric parser turned into a compile‑time operator""_3 template with overflow checks via static_assert, an IP v4/v6 detector that selects return type, a compile‑time fixed‑point constructor, and chrono/unit examples. Covers practical tools—consteval vs constexpr, non‑type char template packs, namespace literal idiom—and failures like implicit octal confusion and negation-before‑UDL producing below‑zero temperatures. C++17 recipes and C++20 improvements are contrasted; implementations and pitfalls are shown, not just theory.
AI-written summary · about
chapters (36)
- 0:00Start
- 1:10literal in C++
- 3:48About me
- 4:22Coming soon to a bookstore near you!
- 5:21What is a user-defined literal (UDL)?
- 7:20Anatomy of a UDL
- 8:55Restrictions on UDLS
- 12:10So, aren't UDLs just syntactic sugar?
- 12:57The UDL operator
- 13:40Namespaces for literals
- 14:54Cooked UDL operators
- 17:46Cooked UDL examples
- 23:13Raw UDL operator example: Base 3 int
- 25:41Base 3 int with error detection
- 28:00Force compile-time error detection
- 28:49Raw UDL operators: Nota Bene
- 29:53Numeric UDL operator templates
- 32:01Base 3 int revisited (helper templates)
- 36:28UDL operator templates: Nota Bene
- 38:10String UDL operator templates
- 39:47Example of a usable structural type
- 43:09IP addresses (continued)
- 44:22About UDLs in the standard library
- 45:06String UDLS
- 45:42Imaginary number UDLS
- 46:53Chrono duration UDLS
- 48:21The problem: type sinks
- 50:05Strong typedefs: the solution
- 52:20Extended numeric types
- 54:36Special-format string-like classes
- 55:06Physical units (continued)
- 56:09Can you obfuscate that better?
- 57:09A lot of work to support "magic" values
- 58:26Wait for the sign
- 59:13Aside: points vs. deltas
- 59:34Conclusions
related talks
01h 05mC++ Lambda Idioms
04mConstexpr and operator overloading: Why everything is terrible
23mTruthiness, falsiness and other JavaScript gotchas
01h 05mBusy TypeScript Developer’s Guide to Advanced TypeScript
51mJavaScript Gotchas
24mTypeScript for JavaScripties
01h 02mJS UX: Writing Code for Humans
11mUnderstanding the differences is accepting
