考虑下面的类:
template<bool Condition> class MyClass
{
protected:
/* SOMETHING */ _var;
};
带有std::conditional<Condition, const int, int>::type _var;
我可以选择是否 _var
通过模板参数是 const 或非常量。
如何对静态/非静态进行等效操作?
(无论你想要什么元编程技术,我都要求一个等效的)
最佳答案
您可能必须使用辅助结构来完成此操作,因为 static 不是类型的一部分,而是存储说明符。例如:
template <class T, bool Static>
struct StaticSelector
{
T value;
};
template <class T>
struct StaticSelector<T, true>
{
static T value;
};
template<bool Condition> class MyClass
{
protected:
StaticSelector<float, Condition> _var;
};
话虽这么说,在静态和非静态之间轻松切换可能不是一个好主意。
关于c++ - 有条件静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12606534/