sbabbi
sbabbi

Reputation: 11181

Define variable template in a class template

Consider this minimal example:

template<class X> struct foo;

template<>
struct foo<int>
{
    template<class = void>
    static constexpr int x = 0;
};

template<class T>
constexpr int foo<int>::x<T>;

The last two lines are needed because otherwise we will get an undefined reference when the variable x is ODR-used.

While gcc (6.2.1) is happy to compile this code, clang (3.9.0) fails on the last line with this cryptic message:

error: variable template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list

Which one is the correct behavior?

Upvotes: 0

Views: 82

Answers (1)

Danh
Danh

Reputation: 6016

The last 2 lines of code should be:

template<class T>
const int foo<int>::x;

or

template<class T>
constexpr int foo<int>::x;

likes this

Upvotes: 4

Related Questions