Massimo Castrioto
Massimo Castrioto

Reputation: 25

Why can't I declare a static constexpr variable in a template class using msvc?

I have a template class, and I want to declare a static constexpr variable of the same type of the class. With gnu compiler it works just fine, but with Microsoft Visual Studio, it won't compile. Am I doing something wrong and it is jest the gnu compiler that is very kind with me, or is it the Microsoft compiler that is at fault? I know I can fix it changing the variable for a function that does the same thing, but I'm curious.

template <typename T>
constexpr T One() noexcept { return static_cast<T>( 1 ); }

template <typename T>
struct Test {
    T val;

    static constexpr Test example{ One<T>() };                    // compiles only with gnu
    static constexpr Test Example() { return Test{ One<T>() }; }  // compiles with both gnu and microsoft
};

The given error (Visal Studio 2017) is :

error C2017 : use of undefined type 'Test'

Upvotes: 0

Views: 223

Answers (1)

Soonts
Soonts

Reputation: 21926

Before the last } your Test template type is incomplete.

Here’s very similar question which adds third compiler. As you see, the answer says VC++ and clang respect the standard, gcc doesn’t.

Upvotes: 1

Related Questions