在c++11之前,在类中定义编译期常量的方法:
template<typename Type>
struct Size
{
static const int x = 1;
};
template<typename Type>
struct Size
{
enum{ x = 1, y = 2 };
};
在c++11中定义编译期常量,无须定义static const或enum类型,只需从std::integral_constant派生
template<typename Type>
struct Size : std::integral_constant<int, 1>
{
};
根据Size::value获取常量1