Partial Specialization Calculation of Template Argument

#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 conclusion normalmeans that the usual (non-specialized) template is selected. This may seem obvious right away, as it int &comes as an argument to the template, and the only specialization available is one that accepts an rvalue reference. But why the second specialization of the template cannot be selected by choosing T = int&, in which case the collapse reduction leads to what T&&will become int&?

+4
source share

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


All Articles