Re: Surprising constructor call
[Another attempt to reply after some weeks]
{ We've made some progress in trying to find out what's causing these
issues, but we're not quite there yet. In the meantime, if you can,
please try to avoid posting from a '@gmail.com' email address.
Thanks! -mod/we }
On 2012-08-13 04:48, Kaba wrote:
> Consider the following code. I'd expect it to call the
> copy-constructor. However, both the Visual Studio 2010 and the
> gcc-compiler at Ideone run the third constructor.
Yes this is required behaviour.
> What are the rules in this case?
There is no much mystery involved here, the rules are based on normal
overload selection rules, see below.
> #include <iostream>
>
> template <typename Type>
> class A
> {
> public:
> A() {}
>
> A(const A& that)
> {
> std::cout << "Copy" << std::endl;
> }
>
> A(A&& that)
> {
> std::cout << "Move" << std::endl;
> }
>
> template <typename That>
> A(That&& that)
> {
> std::cout << "Wait... what?" << std::endl;
> }
> };
>
> int main()
> {
> A<int> a;
> A<int> b(a);
>
> return 0;
> }
You are providing an lvalue of type (non-const) A<int> to an
direct-initialization context. The compiler sees
A<int>::A(const A&) // Copy constructor
A<int>::A(A&&) // Move constructor
and additionally would deduce
A<int>::A<A&>(A&) // No copy constructor, albeit it has the
// same effective signature than A<int>::A(A&)
from the template constructor based on the non-const lvalue of A<int>.
The last one is a better match than any of the first two, so the usual
function-over-template rule cannot be applied here.
Note that the last signature is no copy-constructor, because
copy/move-constructors won't be instantiated from templates (see 12.8
p2+3).
Note also that the restriction rule 12.8 p6 does not apply here,
because the instantiated signature is valid.
As a general rule, you should always constrain a perfect forwarding
constructor and assignment operator with a single argument unless you
know pretty well what you are doing. In this case you should at least
"filter away" all argument types that would correspond with the class
template type (ignoring cv-qualifiers).
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]