1 #include <iostream> 2 3 template<class T, T v> 4 struct integral_constant { 5 static constexpr T value = v; 6 using value_type = T; 7 using type = integral_constant; // 使用注入类名 8 constexpr operator value_type() const noexcept { 9 return value; 10 } 11 constexpr value_type operator()() const noexcept { 12 return value; // C++14 起 13 } 14 }; 15 16 int main() { 17 std::cout << "integral_constant<int, 5> == " 18 << std::integral_constant<int, 5>::value << std::endl; 19 std::cout << "integral_constant<bool, false> == " << std::boolalpha 20 << std::integral_constant<bool, false>::value << std::endl; 21 22 return (0); 23 }