Vincent
Vincent

Reputation: 60421

Conditional static?

Consider the following class :

template<bool Condition> class MyClass
{
    protected:
        /* SOMETHING */ _var;
};

With a std::conditional<Condition, const int, int>::type _var; I can choose if _var is a const or a non-const through the template parameter.

How to do the equivalent for static/non static ?

(I ask for an equivalent though whatever metaprogramming technique you want)

Upvotes: 2

Views: 490

Answers (1)

ltjax
ltjax

Reputation: 16007

You probably have to do it using a helper struct, since static is not part of the type but a storage specifier. For example:

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;
};

That being said, easy switching between static and non-static is probably a bad idea..

Upvotes: 2

Related Questions