The output of the following program…
#include <iostream>
using namespace std;
struct X
{
X(const X&) { cout << "copy" << endl; }
X(X&&) { cout << "move" << endl; }
template<class T> X(T&&) { cout << "tmpl" << endl; }
};
int main()
{
X x1 = 42;
X x2(x1);
}
is
tmpl
tmpl
The desired output is:
tmpl
copy
Why doesn’t the concrete copy constructor take precedence over the template constructor?
Is there anyway to fix it so that the copy and move constructor overloads will take precedence over the template constructor?
Normal overload resolution rules still apply when choosing the constructor – and a constructor taking a non-const lvalue reference (for the template constructor after argument deduction) is a better match than a constructor taking a const lvalue reference.
You could, of course, just add another overload taking a non-const lvalue reference, i.e.
Update: Other cases where the template constructor is a better match: