Data in the Type System: Complex Non-Type Template Parameters in C++20
summary
C++20 adds class-type NTTPs so class objects can be template arguments (removing the need for the non-standard "forbidden UDL"), with requirements: literal type, trivial destructor, strong structural equality, and the compiler emits an lvalue object into the binary for each distinct value. Practical pieces include a fixed_string type and a type-safe printf built with template-parsed format strings that compiles and works; compiler behavior varies (GCC/Clang earlier showed ~50 bytes per char and ~278 bytes per char for the raw UDL before optimizations), and some compilers still miss restriction checks.
AI-written summary · about
chapters (15)
- 0:00Introduction
- 1:09Forbidden UDL and use cases
- 5:49Committee concerns and history
- 7:16Template vs function parameters
- 10:13Type-safe printf wrapper
- 18:45C++17 limitations
- 20:11Type equality and identity
- 27:25Class type NTT P behavior
- 31:16Fixed string in C++20
- 35:15User-defined literals with classes
- 39:54Caveats and future changes
- 40:39Memory usage benchmarks
- 45:48Compiler optimizations
- 46:54Updated printf example
- 50:00Q&A
related talks
01h 21mWhat Has C++20 Ever Done For Templates?
01h 30mRethinking the Way We Do Templates in C++
01h 27mC++20 Templates - the Next Level: Concepts and More
04mFun? with NTTPs in C++
05mtemplate<template...>
57mIntroduction to C++ Template Metaprogramming
05mC++20 Lambdas: Familiar Template Syntax
56m“Type Traits - what are they and why should I use them?"
