mardi 22 décembre 2015

Template argument deduction in partial specialization

#include <iostream>

using namespace std;

template <typename T> class A{
public:
    void test() { cout << "normal" << endl;}

};

//template <typename T> class A<T&>{
//public:
//    void test() { cout << "&" << endl;}

//};

template <typename T> class A<T&&>{
public:
    void test() { cout << "&&" << endl;}

};

int main(){
    A<int&> a;
    a.test();
}

The output is normal suggesting the ordinary (non-specialised) template was chosen. This may be seem obvious straight away since int & is supplied as a template argument and the only available specialisation is one that takes an rvalue reference. But why is it that the second template specialisation cannot be selected by choosing T = int& in which case reference collapsing causes T&& to become int&?

Aucun commentaire:

Enregistrer un commentaire