Skip to content

C++:type traits

This header defines a series of classes to obtain type information on compile-time.

The header contains:

  • Helper classes: Standard classes to assist in creating compile-time constants.
  • Type traits: Classes to obtain characteristics of types in the form of compile-time constant values.
  • Type transformations: Classes to obtain new types by applying specific transformations to existing types.

Helper classes

  • integral_constant
  • true_type
  • false_type

Type traits

Primary type categories

  • is_array
  • is_class
  • is_enum
  • is_floating_point
  • is_function
  • is_integral
  • is_lvalue_reference
  • is_member_function_pointer
  • is_member_object_pointer
  • is_pointer
  • is_rvalue_reference
  • is_union
  • is_void

Composite type categories

  • is_arithmetic
  • is_compound
  • is_fundamental
  • is_member_pointer
  • is_object
  • is_reference
  • is_scalar

Type properties

  • is_abstract
  • is_const
  • is_empty
  • is_literal_type
  • is_pod
  • is_polymorphic
  • is_signed
  • is_standard_layout
  • is_trivial
  • is_trivially_copyable
  • is_unsigned
  • is_volatile

Type features

  • has_virtual_destructor
  • is_assignable
  • is_constructible
  • is_copy_assignable
  • is_copy_constructible
  • is_destructible
  • is_default_constructible
  • is_move_assignable
  • is_move_constructible
  • is_trivially_assignable
  • is_trivially_constructible
  • is_trivially_copy_assignable
  • is_trivially_copy_constructible
  • is_trivially_destructible
  • is_trivially_default_constructible
  • is_trivially_move_assignable
  • is_trivially_move_constructible
  • is_nothrow_assignable
  • is_nothrow_constructible
  • is_nothrow_copy_assignable
  • is_nothrow_copy_constructible
  • is_nothrow_destructible
  • is_nothrow_default_constructible
  • is_nothrow_move_assignable
  • is_nothrow_move_constructible

Type relationships

  • is_base_of
  • is_convertible
  • is_same

Property queries

  • alignment_of
  • extent
  • rank

Type transformations

Const-volatile qualifications

  • add_const
  • add_cv
  • add_volatile
  • remove_const
  • remove_cv
  • remove_volatile

Compound type alterations

  • add_pointer
  • add_lvalue_reference
  • add_rvalue_reference
  • decay
  • make_signed
  • make_unsigned
  • remove_all_extents
  • remove_extent
  • remove_pointer
  • remove_reference
  • underlying_type

Other type generators

  • aligned_storage
  • aligned_union
  • common_type
  • conditional
  • std::enable_if
  • result_of

Example

보통 아래와 같이 static_assert와 함께 사용한다.

static_assert(std::is_same<Path::BaseString, strings::BaseString>::value, "This is not a same type.");

Exists method

Yes, with SFINAE you can check if a given class does provide a certain method. Here's the working code:

#include <iostream>

struct Hello
{
    int helloworld() { return 0; }
};

struct Generic {};    

// SFINAE test
template <typename T>
class has_helloworld
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( typeof(&C::helloworld) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

int main(int argc, char *argv[])
{
    std::cout << has_helloworld<Hello>::value << std::endl;
    std::cout << has_helloworld<Generic>::value << std::endl;
    return 0;
}

String Literal Type Traits

decltype("some string") of a string literal returns const char (&)[n] type. Thus, it seems there is more succinct, in comparison with the following answer, way to detect it:

template<typename T>
struct IsStringLiteral :
    std::is_same<
        T,
        std::add_lvalue_reference_t<const char[std::extent_v<std::remove_reference_t<T>>]>
    >
{};

Variadic Templates Type Traits

You can write a meta-function to determine if all are POD types:

template <typename... Ts>
struct all_pod;

template <typename Head, typename... Tail>
struct all_pod<Head, Tail...>
{
    static const bool value = std::is_pod<Head>::value && all_pod<Tail...>::value;
};

template <typename T>
struct all_pod<T>
{
    static const bool value = std::is_pod<T>::value;
};

then

static_assert( all_pod<Args...>::value, "All types must be POD" );

See also

Favorite site

References


  1. Devpia-TR1-type_traits.pdf