liveBook · Manning

Type trait

As explained in Modern C, Third Edition

A type trait is a compile-time operation that queries a property of a type and produces a result representing that property.

A type trait is a compile-time operation that queries a property of a type and produces a result representing that property. In C, typeof, typeof_unqual, and _Generic can combine to implement type-generic features. section 18.3.2

Type-directed power

A pow macro can inspect the types of X and Y with _Generic, select an integer or floating-point library function, and use typeof_unqual to cast the result to the narrowest suitable unqualified floating-point type. section 18.3.2

Type-trait macros are constrained to accept either an expression without an exposed comma or a type name and produce bool integer constant expressions.

  • Use tozero and tonull to create a zero or null pointer without evaluating the argument. section 18.3.2
  • Test integer types, variable-length arrays, and const- or volatile-qualified pointer targets. section 18.3.2
 #define static_assert_compatible(A, B, REASON)  \
   static_assert(_Generic((typeof(A)*)nullptr,   \
                          typeof(B)*: true,      \
                          default: false),       \
                 "expected compatible types: " REASON ", have " #A " and " #B "")

A static assertion can compare types by applying _Generic to a pointer formed from typeof(A) and matching typeof(B)*. section 18.3.2

How Modern C, Third Edition covers it

How deep it goes
Develops macro requirements and implementation strategies for constants, integer types, VLAs, and pointer qualifiers.
Examples
Uses tozero, tonull, isinteger, isvla, pointer-qualification tests, and a type-directed pow macro.
What it assumes
Assumes familiarity with typeof, _Generic, constant expressions, arithmetic types, VLAs, and pointer qualification.
What you can do after
Build compile-time type-trait macros that return boolean integer constant expressions without evaluating their arguments.
How it connects
Connects typeof, typeof_unqual, _Generic, integer constant expressions, variable-length arrays, and pointer type qualifiers.

Also explained in

Compare how 2 books explain type trait →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.