也许将 has_property 值添加到您的基本模板参数对您有用:
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 > {
};
[更新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 > {
};