C++: Be type-safe - The journey of determining the number of elements in an array
summary
Goal is a drop‑in, type‑safe way to get an array element count that fails at compile time. Implementation evolves from a macro to a constexpr function template that takes a const reference to T[N], then to a decltype/std::extent based approach combining std::extent + remove_reference and static_assert for clear errors. Pitfalls shown: sizeof trick misreports pointers, taking an address forces runtime, missing out‑of‑class static defs (C++11 vs C++17), and unit tests reveal edge cases; final options: std::array, span or the robust header-only helper that works across standards.
AI-written summary · about
chapters (7)
- 0:00Start
- 7:57Type Safety
- 15:29Bonus Slides
- 27:31Error Message
- 35:40Final Solution
- 38:25Resize Helper
- 41:11Counting Array Elements at Compile Time
related talks
05mVector to Array
01h 02mEmbedded Friendly C++: Features That Make a Difference
01h 05mC++11 in the Wild: Techniques from a Real Codebase
01h 05mThe real problem of C++
01h 02mC++20 - My Favorite Code Examples
01h 00mC++ Concepts and Ranges
55mThe Curiously Recurring Pattern of Coupled Types
01h 30mC++ in Constrained Environments
