c++Type alias names for static member variables in template classes

Viewed 41

So I've created a little demo code to show my question (view on godbolt):

#include <vector>

template <class T>
struct Test {
    using Vec = std::vector<T>;
    static Vec data; 
};

template <class T>
Test<T>::Vec data = {};

This gives the following error:

<source>:10:1: error: missing 'typename' prior to dependent type name 'Test<T>::Vec'

Test<T>::Vec data = {};

^~~~~~~~~~~~

I've tried a few different permutations with no luck.

For instance, this version seems to work with gcc and in Visual Studio 2017 (view on godbolt):

#include <vector>

template <class T>
struct Test {
    template <class U>
    using Vec = std::vector<T>;
    static Vec<T> data;
};

template <class T>
Test<T>::Vec<T> data = {};

However, clang doesn't like it.

Then stuff like (view on godbolt):

#include <array>

struct SomeStruct {
    static constexpr auto max_size() {
        return sizeof(SomeStruct);
    }
};

template <class T>
struct Test {
    using Arr = std::array<unsigned, SomeStruct::max_size()>;
    static Arr data;
};

template <class T>
Test<T>::Arr data = {};

Nothing likes, but then if you change Arr to (view on godbolt):

template <class T>
struct Test {
    template <class U>
    using Arr = std::array<unsigned, SomeStruct::max_size()>;
    static Arr<T> data;
};

template <class T>
Test<T>::Arr<T> data = {};

gcc and Visual Studio are happy, but clang still doesn't like it.

So my question is, what is the rule of thumb when creating a type alias for static member variables, and how would I go about fixing the above so they work properly on all compilers? (c++17 and above is fine)

0 Answers
Related