http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46004
Summary: [C++0x] template constructor used to copy object
Product: gcc
Version: 4.6.0
Status: UNCONFIRMED
Keywords: rejects-valid
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
CC: [email protected]
[class.copy]p7 says "A member function template is never instantiated to
perform the copy of a class object to an object of its class type."
struct B
{
int i;
template<typename I>
explicit
B(I&& ii) : i(ii) { }
B(const B&) = default;
};
int main()
{
B b(0);
B bb(b);
}
cc.cc: In constructor 'B::B(I&&) [with I = B&]':
cc.cc:15:11: instantiated from here
cc.cc:7:25: error: cannot convert 'B' to 'int' in initialization
Even with an explicitly declared copy constructor the template is still chosen.
This only happens if the template constructor parameter type is an
rvalue-reference, so only affects C++0x mode.
In http://gcc.gnu.org/viewcvs?view=revision&revision=165144 I had to workaround
this behaviour by replacing the template constructor by a pair of overloads,
one for lvalues and one for rvalues:
explicit B(const int& ii) : i(ii) { }
explicit B(int&& ii) : i(ii) { }