Johann Studanski
Johann Studanski

Reputation: 1073

Template class with std::enable_if_t, static const member initialization

Okay, after searching myself half to death and not finding any answer that actually seemed to work I gotta ask:

Say I've got a class (contrived example, but hopefully good enough for now)

template <typename T, std::enable_if_t< MyConditional<T>::value >>
class MyClass
{
public:
    static const MyClass ZeroInited;

    MyClass(int x, int y) 
        : m_X(x)
        , m_Y(Y) 
    {
    }

    ...
};

How do I properly initialize ZeroInited*? The correct syntax just eludes me (or maybe I'm just too tired), and letting tools "Create Implementation" does not produce correct output either. It's

Thanks everyone!

*) PS: in the templated case, not for a specialization. If I leave out the enable_if_t it's easy:

template <typename T> const MyClass<T> MyClass<T>::ZeroInited {0, 0};

but I can't figure out how to change it once enable_if_t comes into play.

Upvotes: 1

Views: 481

Answers (2)

Jarod42
Jarod42

Reputation: 217145

Issue with

template <typename T, std::enable_if_t< MyConditional<T>::value >>

is that you expect as second argument a void value, which doesn't exist.

  • use

    template <typename T, std::enable_if_t< MyConditional<T>::value, bool> = false>
    class MyClass{/*..*/};
    

    and

    template <typename T, std::enable_if_t< MyConditional<T>::value, bool > B>
    const MyClass<T, B> MyClass<T, B>::ZeroInited{0, 0};
    

    Demo

  • or

    template <typename T, typename /*U*/ = std::enable_if_t< MyConditional<T>::value>>
    class MyClass{/*..*/};
    

    and

    template <typename T, typename U>
    const MyClass<T, U> MyClass<T, U>::ZeroInited{0, 0};
    

    Demo

Upvotes: 3

catnip
catnip

Reputation: 25388

If you can use C++17, you can declare ZeroInited inline and initialise it in the declaration (since this is then also a definition):

static const inline MyClass ZeroInited { 10, 20 };

Live demo

I'm not sure how you solve this in C++14 and earlier.

Upvotes: 3

Related Questions