std::integral_constant
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <type_traits>
  | 
||
|   template< class T, T v > struct integral_constant;  | 
(C++11 起) | |
std::integral_constant 包装特定类型的静态常量。它是 C++ 类型特征的基类。
添加 integral_constant 的特化的程序行为未定义。 
辅助模板为  
  | 
(C++17 起) | 
为其中 T 为 bool 的二种常用情形提供 typedef : 
|   定义于头文件  
<type_traits>  | |
| 类型 | 定义 | 
  true_type
 | 
std::integral_constant<bool, true> | 
  false_type
 | 
std::integral_constant<bool, false> | 
成员类型
| 类型 | 定义 | 
  value_type
 | 
  T
 | 
  type
 | 
std::integral_constant<T,v> | 
成员常量
| 名称 | 值 | 
|    constexpr T value [静态]  | 
  T 类型的值为 v 的静态常量 (公开静态成员常量)  | 
成员函数
|     operator value_type  | 
  返回包装的值  (公开成员函数)  | 
|     operator() (C++14)  | 
  返回包装的值  (公开成员函数)  | 
std::integral_constant::operator value_type
|   constexpr operator value_type() const noexcept;  | 
||
转换函数。返回包装的值。
std::integral_constant::operator()
|   constexpr value_type operator()() const noexcept;  | 
(C++14 起) | |
返回包装的值。此函数允许 std::integral_constant 被用作编译时函数对象的源。
可能的实现
template<class T, T v> struct integral_constant { static constexpr T value = v; using value_type = T; using type = integral_constant; // 使用注入类名 constexpr operator value_type() const noexcept { return value; } constexpr value_type operator()() const noexcept { return value; } // C++14 起 };  | 
示例
运行此代码
#include <iostream> #include <type_traits> int main() { typedef std::integral_constant<int, 2> two_t; typedef std::integral_constant<int, 4> four_t; // static_assert(std::is_same<two_t, four_t>::value, // "two_t and four_t are not equal!"); // error: static assertion failed: "two_t and four_t are not equal!" static_assert(two_t::value*2 == four_t::value, "2*2 != 4" ); 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; // static_assert(my_e_e1::value == my_e::e2, // "my_e_e1::value != my_e::e2"); // error: static assertion failed: "my_e_e1::value != my_e::e2" static_assert(std::is_same<my_e_e1, my_e_e2>::value, "my_e_e1 != my_e_e2"); }