在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
C++11编译期常量定义
本文介绍在C++11之前和之后,如何在类中定义编译期常量。C++11引入了std::integral_constant简化这一过程。
199

被折叠的 条评论
为什么被折叠?



