Compiler Support for type_traits

Document Number N2518=08-0028
2008-02-01
James Widman <widman at gimpel.com>

Background and Rationale


The <type_traits> header [20.4 meta] assumes compiler support for several of the trait templates. For example, the template std::has_trivial_assign cannot be implemented without compiler support. Every compiler that currently provides such support does so in the form of built-in operators that are similar in grammar to the sizeof() operator. (E.g. in the case of has_trivial_assign, there is a corresponding keyword __has_trivial_assign which is used to form the constant expression __has_trivial_assign(T).) Each of the operators proposed here is used to form a constant expression of type bool.

These operators have an odd status: they are needed in order to implement the Standard Library, yet they are not mentioned in any part of the current working draft. So when someone asks whether they are part of Standard C++, one valid answer might be "yes and no, but more importantly, yes". I propose we change that answer to a plain "yes" and formally adopt them as part of the core language. If we have a common spelling and semantics for these operators, Standard Library implementations will be more portable.

Additionally, we might consider changing the text of 20.4 so that the type trait templates that require compiler support are defined in terms of these operators. However, such wording is not included in this version of this proposal.


Proposed wording changes

Add the following to the table of keywords in 2.11 lex.key:

__is_member_object_pointer
__is_member_function_pointer
__is_enum
__is_union
__is_class
__is_function
__is_standard_layout
__is_pod
__is_empty
__is_polymorphic
__is_abstract
__has_trivial_default_ctor
__has_trivial_copy_ctor
__has_trivial_assign
__has_trivial_dtor
__has_nothrow_default_ctor
__has_nothrow_copy_ctor
__has_nothrow_assign
__has_virtual_dtor
__is_base_of
__is_convertible

Add the following to the grammar definition of unary-expression in 5.3 expr.unary p1:

unary-type-trait ( type-id )
binary-type-trait ( type-id, type-id )

Add the following grammar entries:

unary-type-trait:
__is_member_object_pointer
__is_member_function_pointer
__is_enum
__is_union
__is_class
__is_function
__is_standard_layout
__is_pod
__is_empty
__is_polymorphic
__is_abstract
__has_trivial_default_ctor
__has_trivial_copy_ctor
__has_trivial_assign
__has_trivial_dtor
__has_nothrow_default_ctor
__has_nothrow_copy_ctor
__has_nothrow_assign
__has_virtual_dtor
binary-type-trait:
__is_base_of
__is_convertible

Add the following as a subsection at the end of 5.3 expr.unary:

The type trait operators each yield a constant value of type bool indicating whether the type operands have the trait indicated by the trait keyword.
For types T and U, the following holds:

Acknowledgements

Many thanks to Howard Hinnant for providing base document for this proposal and for explaining why __is_convertible is defined the way it is.