Embracing User-Defined Literals Safely
summary
Explains concrete rules and tradeoffs for C++ user-defined literals: four forms (cooked, raw, numeric-template, string-template), 12 cooked overloads, and when the compiler versus the library must parse or detect overflow. Gives working examples (temperature, IPv4, base‑3 parser, chrono units), shows pitfalls like negatives are not part of the literal and namespace lookup collisions, and recommends consteval in C++20 to force compile‑time errors. Demonstrates what to build, how it’s parsed, when it fails, and when factory functions or typedefs are safer.
AI-written summary · about
chapters (21)
- 0:00Intro
- 1:20Literal Examples
- 3:36Outline
- 4:44About me
- 6:27Book
- 8:49UserDefined Literals
- 9:39Why UserDefined Literals
- 11:38Common UserDefined Literals
- 14:25Other Restrictions
- 18:26Factory Functions
- 19:30UserDefined Literal
- 21:03syntactic sugar
- 22:27how to define
- 25:47namespaces
- 28:43cooked udl operators
- 37:39token example
- 42:27possible overloads
- 42:56cooked literals
- 47:48raw udl operators
- 51:05base3 udl
- 54:16base3 error detection
related talks
57mProgramming for Warm Days: Avoiding Dangerous Conversions
05mLightning Talk: C++ String Literals Have the Wrong Type
54mTypical C++, But Why?
51mTypical C++, but Why?
51mThe C++ Type System is your Friend
01h 02mC++20 - My Favourite Code Examples
01h 00mHow To Use `constexpr` In C++23
01h 10mThe C++ Type System Is Your Friend
