Partial specialization of variable templates and constexpr

I am trying to understand patterns and patterns of variables. Consider this:

template<int M, int N>
const int gcd1 = gcd1<N, M % N>;

template<int M>
const int gcd1<M, 0> = M;

std::cout << gcd1<9, 6> << "\n";

He prints 0, which is wrong. However, if I use constexprinstead of the constabove, I get the correct answer 3. I get the correct answer again with the structure template:

template<int M, int N>
struct gcd2 {
    static const int value = gcd2<N, M % N>::value;
};

template<int M>
struct gcd2<M, 0> {
    static const int value = M;
};
std::cout << gcd2<9, 6>::value << "\n";

What am I doing wrong?

Edit: gcd1Also compiles fine without specialization in the base package. How so? I am using Visual Studio 2015.

+4
source share
1 answer

I assume this is a bug in the MSVC compiler.

MSVC 2015 2. , 3.

gcc 6.1: wandbox

+2

Source: https://habr.com/ru/post/1665465/


All Articles