2.11 Summary
A number of techniques form the building blocks of the components presented in this book. Most of the techniques are related to template code.
Compile-time assertions (Section 2.1) help libraries to generate meaningful error messages in templated code.
Partial template specialization (Section 2.2) allows you to specialize a template, not for a specific, fixed set of parameters, but for a family of parameters that match a pattern.
Local classes (Section 2.3) let you do interesting things, especially inside template functions.
Mapping integral constants to types (Section 2.4) eases the compile-time dispatch based on numeric values (notably Boolean conditions).
Type-to-type mapping (Section 2.5) allows you to substitute function overloading for function template partial specialization, a feature missing in C++.
Type selection (Section 2.6) allows you to select types based on Boolean conditions.
Detecting convertibility and inheritance at compile time (Section 2.7) gives you the ability to figure out whether two arbitrary types are convertible to each other, are aliases of the same type, or inherit one from the other.
TypeInfo (Section 2.8) implements a wrapper around std::type_info, featuring value semantics and ordering comparisons.
The NullType and EmptyType classes (Section 2.9) function as placeholder types in template metaprogramming.
The TypeTraits template (Section 2.10) offers a host of general-purpose traits that you can use to tailor code to specific categories of types.
Table 2.1. TypeTraits<T> Members
isPointer |
Boolean constant |
True if T is a pointer. |
PointeeType |
Type |
Evaluates to the type to which T points, if T is a pointer. Otherwise, evaluates to NullType. |
isReference |
Boolean constant |
True if T is a reference. |
ReferencedType |
Type |
If T is a reference, evaluates to the type to which T refers. Otherwise, evaluates to the type T itself. |
ParameterType |
Type |
The type that's most appropriate as a parameter of a nonmutable function. Can be either T or const T&. |
isConst |
Boolean constant |
True if T is a const-qualified type. |
NonConstType |
Type |
Removes the const qualifier, if any, from type T. |
isVolatile |
Boolean constant |
True if T is a volatile-qualified type. |
NonVolatileType |
Type |
Removes the volatile qualifier, if any, from type T. |
NonQualifiedType |
Type |
Removes both the const and volatile qualifiers, if any, from type T. |
isStdUnsignedInt |
Boolean constant |
True if T is one of the four unsigned integral types (unsigned char, unsigned short int, unsigned int, or unsigned long int). |
isStdSignedInt |
Boolean constant |
True if T is one of the four signed integral types (signed char, short int, int, or long int). |
isStdIntegral |
Boolean constant |
True if T is a standard integral type. |
isStdFloat |
Boolean constant |
True if T is a standard floating-point type (float, double, or long double). |
isStdArith |
Boolean constant |
True if T is a standard arithmetic type (integral or floating point). |
isStdFundamental |
Boolean constant |
True if T is a fundamental type (arithmetic or void). |
|