type properties
A type attribute defines a compile-time template-based structure for querying or modifying the properties of a type.
Attempting to specialize a template defined in the <type_traits> header causes undefined behavior, except that std::common_type can be specialized as described.
Templates defined in the <type_traits> header file may be instantiated with incomplete types unless otherwise specified, although instantiation of standard library templates with incomplete types is generally prohibited.
Auxiliary class
std::integral_constant
defined in header file |
|
integral_constant (C++11) bool_constant (C++17) |
Compile-time constant of specified type with specified value (class template) |
The standard provides two specializations of std::integral_constant for type bool:
defined in header file |
|
type | definition |
true_type |
std::integral_constant<bool, true> |
false_type |
std::integral_constant<bool, false> |
template< class T, T v > |
(since C++11) |
std::integral_constant wraps a static constant of a specific type. It is the base class for C++ type attributes.
Help templateHelp alias template
|
(since C++17) |
provides typedefs for two common cases where T
is bool:
defined in header file |
|
type | definition |
true_type |
std::integral_constant<bool, true> |
false_type |
std::integral_constant<bool, false> |
member type
type | definition |
value_type |
T |
type |
std::integral_constant<T,v> |
member constants
name | value |
constexpr T value [static] |
T Static constant of type whose value is (public static member constant)v |
member function
operator value_type |
Return the wrapped value (public member function) |
operator() (C++14) |
Return the wrapped value (public member function) |
std::integral_constant::operator value_type
constexpr operator value_type() const noexcept; |
conversion function. Returns the wrapped value.
std::integral_constant::operator()
constexpr value_type operator()() const noexcept; |
(since C++14) |
Returns the wrapped value. This function allows std::integral_constant to be used as a source of compile-time function objects.
Feasible to implement
template<class T, T v>
struct integral_constant {
static constexpr T value = v;
typedef T value_type;
typedef integral_constant type; // 使用注入的类名
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } // c++14 起
};
Call example
#include <iostream>
#include <type_traits>
int main()
{
std::cout << std::boolalpha;
typedef std::integral_constant<int, 2> two_t;
typedef std::integral_constant<int, 4> four_t;
std::cout << "std::is_same<two_t, four_t>::value: "
<< std::is_same<two_t, four_t>::value << std::endl;
std::cout << "two_t::value * 2 == four_t::value: "
<< (two_t::value * 2 == four_t::value) << std::endl;
enum class my_e
{
e1,
e2
};
typedef std::integral_constant<my_e, my_e::e1> my_e_e1;
typedef std::integral_constant<my_e, my_e::e2> my_e_e2;
std::cout << "my_e_e1::value == my_e::e2: "
<< (my_e_e1::value == my_e::e2) << std::endl;
std::cout << "std::is_same<my_e_e2, my_e_e2>::value: "
<< std::is_same<my_e_e2, my_e_e2>::value << std::endl;
return 0;
}
output
std::is_same<two_t, four_t>::value: false
two_t::value * 2 == four_t::value: true
my_e_e1::value == my_e::e2: false
std::is_same<my_e_e2, my_e_e2>::value: true