~/talk

How to declare a constant in C++

Talkat NDC TechTown 2025 · 2025-09-22..09-25· [rust]
links:none on file
slides:none on file

C++ constants behave differently at compile time and runtime: prefer constexpr whenever possible because it enables compile-time folding and removes duplicate storage; local constants need static for static storage duration to avoid per-call construction and the guard is only paid once; header constants require inline to get one copy across translation units or use extern/definition in a .cpp; tools used include Compiler Explorer, assembly inspection, readelf and nm and -flto to remove duplicate init code; modules change linkage rules and can still hide ODR violations across module implementation units.

AI-written summary · about

  1. 0:00Introduction
  2. 2:16Defining constants in C++
  3. 3:00Prefer constexpr over const
  4. 4:04Local constants optimization
  5. 6:11Local constants with static storage
  6. 9:29Non-literal local constants
  7. 13:09Global constants in CPP files
  8. 17:45Global constants with anonymous namespaces
  9. 18:06Global constants in headers
  10. 20:37Constexpr in headers
  11. 23:55Weak linkage with inline
  12. 25:44Non-literal types in headers
  13. 28:58Link time optimization
  14. 30:25Detaching declaration and definition with extern
  15. 33:36Static data members
  16. 35:05C++ modules intro
  17. 36:29Modules: Interface and implementation
  18. 39:19Modules linkage and constexpr
  19. 41:09ODR violations in modules
  20. 43:17Guidelines for module constants
  21. 45:47Constant compiler quirks
  22. 49:00Summary
  23. 49:43Q&A
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.