C++;派生类型必须定义静态constexpr 有没有一种方法来强制派生类型在C++中定义静态CONSTRPR < /代码>?我有一个基类,我想强制每个派生类定义一个静态常量bool has\u属性
我试着用CRTP来实现这一点(这样每个派生类都有自己的C++;派生类型必须定义静态constexpr 有没有一种方法来强制派生类型在C++中定义静态CONSTRPR < /代码>?我有一个基类,我想强制每个派生类定义一个静态常量bool has\u属性,c++,templates,inheritance,C++,Templates,Inheritance,我试着用CRTP来实现这一点(这样每个派生类都有自己的static const): 模板 结构基{ T数据; 静态constexpr bool具有_属性; }; 模板 结构派生:公共基{ 静态constexpr bool具有_属性=false; }; 但是编译器抱怨Base::has_属性未初始化 如何实现这一点?我们可以在基的构造函数中插入静态断言: template <typename T, class MyClass> struct Base { T data;
static const
):
模板
结构基{
T数据;
静态constexpr bool具有_属性;
};
模板
结构派生:公共基{
静态constexpr bool具有_属性=false;
};
但是编译器抱怨Base::has_属性
未初始化
如何实现这一点?我们可以在
基的构造函数中插入静态断言
:
template <typename T, class MyClass>
struct Base {
T data;
Base() {
static_assert(std::is_same<
typename std::decay<
decltype(MyClass::has_property)
>::type, bool
>::value,
"Must be a bool");
static_assert(MyClass::has_property || true,
"Must be constexpr");
}
};
也许将has_属性值添加到基本模板参数将对您有效:
template <typename T, class MyClass, bool hasPropertyValue>
struct Base {
T data;
static constexpr bool has_property = hasPropertyValue;
};
template <typename T>
struct Derived : public Base<T, Derived<T>, false > {
};
模板
结构基{
T数据;
静态constexpr bool具有_property=hasPropertyValue;
};
模板
结构派生:公共基{
};
[更新1]
对于数组-而不是传递单个布尔值-传递包含值的结构:
template <typename T, class MyClass, class MyClassPropertyValues>
struct Base {
T data;
static constexpr bool has_property[MyClassPropertyValues::length];
};
template <typename T, class MyClass, class MyClassPropertyValues>
constexpr bool Base<T, MyClass, MyClassPropertyValues>::
has_property[MyClassPropertyValues::length] = MyClassPropertyValues::initValues;
struct DerivedPropertyValues {
static constexpr size_t length = 3;
static constexpr bool initValues[length];
};
constexpr bool DerivedPropertyValues::initValues[length] = { true, false, true };
template <typename T>
struct Derived : public Base<T, Derived<T>, DerivedPropertyValues > {
};
模板
结构基{
T数据;
静态constexpr bool具有_属性[MyClassPropertyValues::length];
};
模板
constexpr布尔基::
has_property[MyClassPropertyValues::length]=MyClassPropertyValues::initValues;
结构派生属性值{
静态constexpr size\u t length=3;
静态constexpr bool initValues[length];
};
constexpr bool-DerivedPropertyValues::initValues[length]={true,false,true};
模板
结构派生:公共基{
};
您想要static constexpr
(C++11)还是static const
(C++03)?您缺少constexpr
的类型(大概是bool
)@iammilind我想要static constep
,并且认为允许您在定义中初始化static constexpr
有一些好处?谢谢你的建议。实际上,我试图强制子类型定义指定长度的布尔的静态数组(问题中的布尔只是一个示例)。有什么想法吗?@Oliver:与bool[n]
相比,而不是bool
。好主意,但我实际上想要一个bool数组——如果我将数组作为模板参数传递,你认为它仍然可读吗?添加了属性数组更新。包含布尔数组的结构非常可读,我相信。。。
template <typename T, class MyClass, bool hasPropertyValue>
struct Base {
T data;
static constexpr bool has_property = hasPropertyValue;
};
template <typename T>
struct Derived : public Base<T, Derived<T>, false > {
};
template <typename T, class MyClass, class MyClassPropertyValues>
struct Base {
T data;
static constexpr bool has_property[MyClassPropertyValues::length];
};
template <typename T, class MyClass, class MyClassPropertyValues>
constexpr bool Base<T, MyClass, MyClassPropertyValues>::
has_property[MyClassPropertyValues::length] = MyClassPropertyValues::initValues;
struct DerivedPropertyValues {
static constexpr size_t length = 3;
static constexpr bool initValues[length];
};
constexpr bool DerivedPropertyValues::initValues[length] = { true, false, true };
template <typename T>
struct Derived : public Base<T, Derived<T>, DerivedPropertyValues > {
};