Type Trait Functions Overview (C++11)

From RAD Studio
Jump to: navigation, search

Go Up to Type Trait Functions (C++11) Index

C++Builder 2009 supports a library of type trait functions designed to support compile time metaprogramming techniques.

These type trait functions are intrinsic type functions that are defined in a manner similar to typeid, sizeof, and decltype. The type trait functions accept a type at compile time and deliver a compile time constant expression as a result, typically of type bool.

Each type trait function is named after its respective type trait, prefixed with a double underscore (__), which marks a name reserved to the implementation.

For example, the following is a type trait function that evaluates True if T is a union type, or False otherwise:

bool __is_union(typename T)

The typename keyword here indicates a function that takes a type rather than a value as an argument, for illustration purposes only.

Differences Between Type Trait Functions and Other Intrinsic Functions

Type trait functions accept only named types, not arbitrary expressions (which would be accepted by typeid, sizeof, and decltype). You can specify multiple arguments with a type trait function, but a comma-separated list is interpreted as an application of the comma operator by typeid, sizeof, and decltype.

Similarly, any attempt to reference the type of a bit field (such as through decltype) produces the underlying storage type for the bit field, and that is what is tested in the type trait functions.

Incomplete Types and Type Trait Functions

Many of the type trait functions do work with incomplete types. For example, after a class has been forward declared, the type can be identified as a class, not a union, enum or fundamental type. The class can have references and pointers, and so forth. Therefore, most type trait functions accept two special incomplete types: void and array-of-unknown-bound (of complete types). For these two incomplete types, most type trait functions return False.

However, a few type functions require complete types. For example, a class declaration is not sufficient to know about bases or triviality.

For convenience, the following type trait function is implemented in C++Builder:

__is_complete_type(T)

This type trait function is unique in that it might return different results at compile time depending on its location in the translation unit. This phenomenon makes this type trait function ripe for ODR violations when used carelessly.

A table of type trait functions that require that the type be a complete type, an array of unknown bound, or (possibly cv-qualified) void is given in Table 43: "Type Property Predicates" in Section 20 of the Working Draft.

See Also