Search code examples
c++initializer-liststdarray

Visual Studio std::array initializer bug (2797)


Visual Studio has an issue at the moment such that the following won't compile, giving the error,

error C2797: 'vec::v': list initialization inside member initializer list or non-static data member initializer is not implemented

#include <array>

template<class T, int C>
struct vec
{
    typedef T value_type;

    enum { num_components = C };

    std::array<T, C> v;

    template<typename ...Args>
    vec(Args&&... args) : v{{args...}} {}
};

template<class T>
struct vec2 : vec<T, 2>
{
    vec2(T x, T y) : vec(x, y) {}
};

int main(void)
{
    vec<float, 2> v(10.0f, 20.0f);  
}

The Microsoft Connect ticket for it is closed but there's an MSDN piece about it that recommends, "use explicit construction of inner lists". I don't understand how to do this and the code looks quite alien to me (a beginner).

Can anyone assist with an example using std::array?


Solution

  • In this particular case you can just add a pair of parentheses:

    vec(Args&&... args) : v({{args...}}) {}
    

    This works with VS2013 which, I suppose, you are using.

    With VS2015 the code works without modifications.

    Also note that for C++ conformance vec2 should be rewritten as

    vec2(T x, T y) : vec<T, 2>(x, y) {}
    

    or

    using base = vec<T, 2>;
    vec2(T x, T y) : base(x, y) {}