c++11 编译期常量定义

在c++11之前,在类中定义编译期常量的方法:

template
struct Size
{
    static const int x = 1;
};

template
struct Size
{
    enum{ x = 1, y = 2 };
};

在c++11中定义编译期常量,无须定义static const或enum类型,只需从std::integral_constant派生

template
struct Size : std::integral_constant
{

};

根据Size::value获取常量1

你可能感兴趣的:(C++11)