C++ Flashcards: Type System
C++ — Type System Flashcards
20 interactive flashcards. Press Space to flip, rate 1-4 to schedule next review.
Intuition
C++‘s type system determines what operations are valid on every expression, it’s the set of rules the compiler uses to decide whether your code makes sense. Value categories (lvalue, rvalue, prvalue) describe where an expression can appear and whether it has an identity. const and constexpr add constraints: const means “this won’t change after initialization,” while constexpr means “this can be evaluated at compile time.” Type traits let you query type properties at compile time, enabling template specialisation decisions.
Common Pitfalls
- Implicit conversions: C++ will silently convert between numeric types (
inttofloat, doubletoint), potentially losing precision, usestatic_castexplicitly or[[nodiscard]]with narrow checks. - Undefined behaviour from strict aliasing: Accessing an object through a pointer of an incompatible type (e.g., casting
int*tofloat*) violates the strict aliasing rule and can cause unpredictable results. - Const casting away constness: Removing
constwithconst_castand then modifying the object is undefined behaviour if the original object was declaredconst.
Cross-References
- Data Layout: Stack memory layout and object representation.
- Resource Management: Ownership and RAII concepts that depend on type system features.
- Templates and Metaprogramming: Type traits and template metaprogramming techniques.