dimanche 27 mars 2016

Why can I overload a function with disjoint template parameter options, but not a class?

This compiles:

template <typename T, typename T2> void foo() { std::cout << "in foo() for two types." << std::endl; }
template <typename T, T Value>     void foo() { std::cout << "in foo() for a type and a value." << std::endl; }

This doesn't (GCC 4.9.3 with -std=c++11):

template <typename T, typename T2> class A { A() { std::cout << "in A::A() for two types." << std::endl; } };
template <typename T, T Value>     class A { A() { std::cout << "in A::a() for a type and a value." << std::endl; } };

With the error being:

a.cpp:6:23: error: template parameter ‘class T2’
 template <typename T, typename T2> class A { A() { std::cout << "in A::A() for two types." << std::endl; } };
                       ^
a.cpp:7:42: error: redeclared here as ‘T Value’
 template <typename T, T Value>     class A { A() { std::cout << "in A::a() for a type and a value." << std::endl; } };
                                          ^

The second overload-of-sorts seems perfectly reasonable to me, there can be no ambiguity, since types and values are disjoint. So why is it not allowed?

Aucun commentaire:

Enregistrer un commentaire