3

Why the output is 0003212 ?

#include <iostream>
using namespace std;

template<typename X> class C
{
public:
    C() { cout<<"0";}
    template<class T> C(const C<T>& c) { cout<<"1";}
    C(const C<int>& c) { cout<<"2";}
    template<class T> C(const C<T*>& c) { cout<<"3";}
};

int main(int argc, char* args[])
{
    C<int> c1;          // 0
    C<double> c2;       // 0
    C<int*> c3;         // 0

    C<int> c4(c3);      // 3
    C<int> c5(c1);      // 2
    C<int> c6(c2);      // 1
    C<float> c7(c1);    // 2
    C<double> c8(c2);   // ?

    std::cin.get();
    return 0;
}

What is invoked in the last meaning line?

I can suppose that it's some auto-created ctor but can't figure out which one.

3
  • @H2CO3: Nope, a template constructor can never displace the copy constructor. And the default copy constructor C<X>::C(const C<X>&) is a better match for the c8 initialization than any of the templates.
    – Ben Voigt
    Jan 18, 2014 at 21:58
  • Your C<X>::C(const C<X>&) and my template<class T> C(const C<T>& c) { cout<<"1";} are not the same thing?
    – papirosnik
    Jan 18, 2014 at 22:12
  • No they are not. I'll add an answer explaining why.
    – Ben Voigt
    Jan 18, 2014 at 22:15

3 Answers 3

5

There are several C++ language rules in play here.

  1. A template cannot be a copy constructor. (Standard rule 12.8p2)

    A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.

  2. If no copy constructor is defined, the compiler generates a default one (if possible). (Standard rule 12.8p7)

    If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor. Thus, for the class definition

    struct X {
       X(const X&, int);
    };
    

    a copy constructor is implicitly-declared. If the user-declared constructor is later defined as X::X(const X& x, int i =0) { /∗ ... ∗/ } then any use of X's copy constructor is ill-formed because of the ambiguity; no diagnostic is required.

  3. If a template and non-template are an equally good match for the arguments, the non-template wins. (Standard rule 13.3.3) The rule is a big hard-to-digest mess, I'll show just the important part:

    [...] a viable function F1 is defined to be a better function than another viable function F2 if [...rules about argument matching...] or, if not that, F1 is a non-template function and F2 is a function template specialization [...]

From the code you provided, only

C<int>::C(const C<int>&)

is a user-defined copy-constructor that prints 2. All X other than int don't define a copy-constructor, so the compiler creates one.

See also

10
  • Your rules sound like true ) I continued to experiment and now understand which of ctors is used. Rules that you've written explain my case nice, but one small question... Are they in c++ standratd?
    – papirosnik
    Jan 18, 2014 at 22:33
  • @user2032538: I've paraphrased the Standard to make it easier to understand, but I can find the actual wording in the Standard.
    – Ben Voigt
    Jan 18, 2014 at 22:34
  • This answer is far better than the accepted one... :( Too few upvotes, too much ignorance... :(
    – user529758
    Jan 18, 2014 at 22:38
  • I liked this answer better when my summary of the rules was together :(
    – Ben Voigt
    Jan 18, 2014 at 22:53
  • Thanks for your nice detailed explanation.Now I'm trying to understand this mistery: what is the reason that template constructor cannot be copy-constructor. Why was this condition included into c++ standard...
    – papirosnik
    Jan 18, 2014 at 23:15
4

It's copy constructor generated for you by compiler, and since it's best match it's selected in last case.

2
  • c6 is of type C<int> and c2 is of type C<double>. These are to completely separate classes as far as compiler is concerned. So to find which ctor to use, compiler needs to consider C<int> ctors (generate list of possible matches). First one is obviously not ok. Second one might work. Third one takes different type (C<int>) from actuall parameter (C<double>) so is not ok. Last one also is not ok, since it takes things of form C<T*> which is not matching C<double>. So there is only one possible match for this case.
    – tumdum
    Jan 18, 2014 at 22:10
  • You're right I managed to see the trails of that generated constructor in asm listing. and now I understand what happens under hood ) Thanks a lot
    – papirosnik
    Jan 18, 2014 at 22:20
0

In last case you call default copy constructor.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.