jeudi 29 octobre 2015

const double & in template class partial specialization

Suppose the following definitions:

#define AA(x) A<decltype(x), x>

template<typename T, T x>
struct A;

template<int a>
struct A<int, a>{};

template<bool b>
struct A<bool, b>{};

template<const double &d>
struct A<const double &, d>{};

extern constexpr double myDouble = 0.2;

After these definitions, the following works:

AA(false) myA2; 

However, the compiler complains when it sees this:

AA(myDouble) myA3;

It says:

‘const double’ is not a valid type for a template non-type parameter

#define AA(x) A

On the other hand, the compiler is perfectly happy with this:

A<const double &, myDouble> myA1; 

What is the reason for this compiler error? (using g++ version 4.8.2)

Aucun commentaire:

Enregistrer un commentaire