Why doesn't the standard consider a template constructor as a copy constructor?C++ template copy constructor...

Combinatorics problem, right solution?

How to not starve gigantic beasts

How long after the last departure shall the airport stay open for an emergency return?

How do I reattach a shelf to the wall when it ripped out of the wall?

What is this word supposed to be?

How do I produce this symbol: Ϟ in pdfLaTeX?

What *exactly* is electrical current, voltage, and resistance?

Crossed out red box fitting tightly around image

How bug prioritization works in agile projects vs non agile

Apply a different color ramp to subset of categorized symbols in QGIS?

Why did C use the -> operator instead of reusing the . operator?

What to do with someone that cheated their way through university and a PhD program?

Negative Resistance

What was Apollo 13's "Little Jolt" after MECO?

Is Diceware more secure than a long passphrase?

Help with my training data

Nails holding drywall

Should the Product Owner dictate what info the UI needs to display?

"The cow" OR "a cow" OR "cows" in this context

Find the identical rows in a matrix

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

As an international instructor, should I openly talk about my accent?

How exactly does Hawking radiation decrease the mass of black holes?

Does the damage from the Absorb Elements spell apply to your next attack, or to your first attack on your next turn?



Why doesn't the standard consider a template constructor as a copy constructor?


C++ template copy constructor on template classTemplate “copy constructor” does not prevent compiler-generated move constructorWhy can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?Why is “using namespace std” considered bad practice?Why would a copy constructor have more than one parameter?“Template typedef” inside a copy constructor doesn't workC++ Template constructor, why is copy constructor being called?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsExplicit copy constructor and uniform initializationC++ template copy constructor on template classtemplate constructor one parameter const and not const






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







24















Here's the definition of copy constructor, [class.copy.ctor/1]:




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 ([dcl.fct.default]).




Why does the standard exclude templates as copy constructors?



In this simple example, both constructors are copy constructors:



struct Foo {
Foo(const Foo &); // copy constructor
Foo(Foo &); // copy constructor
};


See this similar example:



struct Foo {
Foo() = default;

template <typename T>
Foo(T &) {
printf("heren");
}
};

int main() {
Foo a;
Foo b = a;
}


In this example, here will be printed. So it seems that my template constructor is a copy constructor, at least it behaves like one (it is called in a context where copy constructors are usually called).



Why is the "non-template" requirement there in the text?










share|improve this question

























  • Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

    – YSC
    14 hours ago













  • @YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

    – Angew
    14 hours ago











  • @Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

    – YSC
    14 hours ago








  • 1





    What happens if you Foo c = std::move(a); ?

    – Caleth
    14 hours ago






  • 1





    @sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

    – geza
    11 hours ago


















24















Here's the definition of copy constructor, [class.copy.ctor/1]:




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 ([dcl.fct.default]).




Why does the standard exclude templates as copy constructors?



In this simple example, both constructors are copy constructors:



struct Foo {
Foo(const Foo &); // copy constructor
Foo(Foo &); // copy constructor
};


See this similar example:



struct Foo {
Foo() = default;

template <typename T>
Foo(T &) {
printf("heren");
}
};

int main() {
Foo a;
Foo b = a;
}


In this example, here will be printed. So it seems that my template constructor is a copy constructor, at least it behaves like one (it is called in a context where copy constructors are usually called).



Why is the "non-template" requirement there in the text?










share|improve this question

























  • Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

    – YSC
    14 hours ago













  • @YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

    – Angew
    14 hours ago











  • @Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

    – YSC
    14 hours ago








  • 1





    What happens if you Foo c = std::move(a); ?

    – Caleth
    14 hours ago






  • 1





    @sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

    – geza
    11 hours ago














24












24








24


5






Here's the definition of copy constructor, [class.copy.ctor/1]:




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 ([dcl.fct.default]).




Why does the standard exclude templates as copy constructors?



In this simple example, both constructors are copy constructors:



struct Foo {
Foo(const Foo &); // copy constructor
Foo(Foo &); // copy constructor
};


See this similar example:



struct Foo {
Foo() = default;

template <typename T>
Foo(T &) {
printf("heren");
}
};

int main() {
Foo a;
Foo b = a;
}


In this example, here will be printed. So it seems that my template constructor is a copy constructor, at least it behaves like one (it is called in a context where copy constructors are usually called).



Why is the "non-template" requirement there in the text?










share|improve this question
















Here's the definition of copy constructor, [class.copy.ctor/1]:




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 ([dcl.fct.default]).




Why does the standard exclude templates as copy constructors?



In this simple example, both constructors are copy constructors:



struct Foo {
Foo(const Foo &); // copy constructor
Foo(Foo &); // copy constructor
};


See this similar example:



struct Foo {
Foo() = default;

template <typename T>
Foo(T &) {
printf("heren");
}
};

int main() {
Foo a;
Foo b = a;
}


In this example, here will be printed. So it seems that my template constructor is a copy constructor, at least it behaves like one (it is called in a context where copy constructors are usually called).



Why is the "non-template" requirement there in the text?







c++ templates constructor language-lawyer copy-constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 10 hours ago









StoryTeller

107k14224288




107k14224288










asked 14 hours ago









gezageza

14.2k33283




14.2k33283













  • Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

    – YSC
    14 hours ago













  • @YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

    – Angew
    14 hours ago











  • @Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

    – YSC
    14 hours ago








  • 1





    What happens if you Foo c = std::move(a); ?

    – Caleth
    14 hours ago






  • 1





    @sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

    – geza
    11 hours ago



















  • Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

    – YSC
    14 hours ago













  • @YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

    – Angew
    14 hours ago











  • @Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

    – YSC
    14 hours ago








  • 1





    What happens if you Foo c = std::move(a); ?

    – Caleth
    14 hours ago






  • 1





    @sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

    – geza
    11 hours ago

















Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

– YSC
14 hours ago







Note: I'm note sure Foo b = a instantiates and calls Foo::Foo<Foo>(Foo&). It might rather call the implicitly declared copy constructor.

– YSC
14 hours ago















@YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

– Angew
14 hours ago





@YSC Both gcc and clang (tested on Coliru) actually do print "here" from this code.

– Angew
14 hours ago













@Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

– YSC
14 hours ago







@Angew maybe they are wrong then, in regard of [class.copy.ctor]/1 & /6.

– YSC
14 hours ago






1




1





What happens if you Foo c = std::move(a); ?

– Caleth
14 hours ago





What happens if you Foo c = std::move(a); ?

– Caleth
14 hours ago




1




1





@sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

– geza
11 hours ago





@sebrockm: the standard specifies what a copy constructor is. It defines, that if a constructor is non-template, and has specific parameters, then it is a copy-constructor. All the other constructors are not copy-constructors.

– geza
11 hours ago












3 Answers
3






active

oldest

votes


















25














Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.



A member template is not a member function. Members are instantiated from it only when needed.



So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.



The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.






share|improve this answer





















  • 1





    What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

    – YSC
    14 hours ago






  • 4





    So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

    – geza
    14 hours ago






  • 1





    @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

    – StoryTeller
    14 hours ago






  • 2





    @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

    – StoryTeller
    14 hours ago






  • 6





    @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

    – geza
    14 hours ago





















9















Why is the "non-template" requirement there in the text?




Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:



struct Foo {
// ctor template: clearly useful and necessary
template <typename T>
Foo(const T&) {}

// copy ctor: same signature! can't work
template <typename T>
Foo(const T &) {}
};


Besides, constructing a Foo from an object that is not a Foo can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).




In this example, here will be printed. So it seems that my template constructor is a copy constructor




The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to



template <typename T>
Foo(const T &) {
// ^^^^^
printf("heren");
}


then Foo b = a; results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:



Foo(const Foo&);


This requires adding a const-qualifier to a in Foo b = a;. The original constructor template Foo(T&) in your snippet is a better match, as no const-qualifier is added.






share|improve this answer





















  • 4





    Ah yes! Indeed the whole const vs non-const thing. +1

    – StoryTeller
    14 hours ago











  • I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

    – geza
    11 hours ago











  • Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

    – lubgr
    11 hours ago











  • @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

    – geza
    11 hours ago



















0














A copy constructor is of the form X(X& ) or (X const&), and it will be provided for you by the compiler if you didn't declare one yourself. Non-template comes here probably due to issues if you use template classes.



Let's say there is a template class that has a template copy constructor. The problem is that when you instantiate that class using another instance of this class with the same template type, your template copy constructor will not be called.



The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution.



Source: C++ template copy constructor on template class






share|improve this answer










New contributor




BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55845896%2fwhy-doesnt-the-standard-consider-a-template-constructor-as-a-copy-constructor%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    25














    Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.



    A member template is not a member function. Members are instantiated from it only when needed.



    So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.



    The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.






    share|improve this answer





















    • 1





      What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

      – YSC
      14 hours ago






    • 4





      So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

      – geza
      14 hours ago






    • 1





      @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

      – StoryTeller
      14 hours ago






    • 2





      @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

      – StoryTeller
      14 hours ago






    • 6





      @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

      – geza
      14 hours ago


















    25














    Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.



    A member template is not a member function. Members are instantiated from it only when needed.



    So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.



    The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.






    share|improve this answer





















    • 1





      What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

      – YSC
      14 hours ago






    • 4





      So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

      – geza
      14 hours ago






    • 1





      @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

      – StoryTeller
      14 hours ago






    • 2





      @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

      – StoryTeller
      14 hours ago






    • 6





      @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

      – geza
      14 hours ago
















    25












    25








    25







    Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.



    A member template is not a member function. Members are instantiated from it only when needed.



    So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.



    The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.






    share|improve this answer















    Let's put templates aside for a second. If a class doesn't declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it's defaulted nonetheless.



    A member template is not a member function. Members are instantiated from it only when needed.



    So how can a compiler know from the class definition alone whether or not a specialization with T = Foo will ever be needed? It can't. But it's exactly that on which it needs to base a decision of how to handle a potential need for an implicitly defaulted copy constructor (AND move constructor). That becomes messy.



    The easiest approach is to exclude templates. We'll always have some copy constructor anyway, it will do the correct thingTM by default, and will be favored by overload resolution because it's not instantiated from a template.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 9 hours ago









    Peter Mortensen

    14k1987114




    14k1987114










    answered 14 hours ago









    StoryTellerStoryTeller

    107k14224288




    107k14224288








    • 1





      What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

      – YSC
      14 hours ago






    • 4





      So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

      – geza
      14 hours ago






    • 1





      @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

      – StoryTeller
      14 hours ago






    • 2





      @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

      – StoryTeller
      14 hours ago






    • 6





      @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

      – geza
      14 hours ago
















    • 1





      What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

      – YSC
      14 hours ago






    • 4





      So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

      – geza
      14 hours ago






    • 1





      @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

      – StoryTeller
      14 hours ago






    • 2





      @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

      – StoryTeller
      14 hours ago






    • 6





      @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

      – geza
      14 hours ago










    1




    1





    What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

    – YSC
    14 hours ago





    What's your opinion on g++ & clang++ printing "here" in the program shown in the Q then?

    – YSC
    14 hours ago




    4




    4





    So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

    – geza
    14 hours ago





    So basically you say that this is because of the rule: "If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly". And we still expect that the usual implicit copy constructor is defined, no matter of the templated constructor. Yep, this seems the explanation.

    – geza
    14 hours ago




    1




    1





    @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

    – StoryTeller
    14 hours ago





    @YSC - Honestly? Feels like a bug to me. I can't experiment more at the moment to comment more about it. This is from my phone.

    – StoryTeller
    14 hours ago




    2




    2





    @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

    – StoryTeller
    14 hours ago





    @YSC- Or maybe... the defaulted one has a const qualified parameter, while the template doesn't. So It's a better match. Does it still happen with T const &?

    – StoryTeller
    14 hours ago




    6




    6





    @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

    – geza
    14 hours ago







    @YSC: it is printed, because the parameter passed is not const, so the template is a better match than the implicit one. But it is confusing (for me) to not call the template specialization as copy constructor. But the standard does this because of StoryTeller's explanation. At least, this is a sensible explanation

    – geza
    14 hours ago















    9















    Why is the "non-template" requirement there in the text?




    Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:



    struct Foo {
    // ctor template: clearly useful and necessary
    template <typename T>
    Foo(const T&) {}

    // copy ctor: same signature! can't work
    template <typename T>
    Foo(const T &) {}
    };


    Besides, constructing a Foo from an object that is not a Foo can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).




    In this example, here will be printed. So it seems that my template constructor is a copy constructor




    The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to



    template <typename T>
    Foo(const T &) {
    // ^^^^^
    printf("heren");
    }


    then Foo b = a; results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:



    Foo(const Foo&);


    This requires adding a const-qualifier to a in Foo b = a;. The original constructor template Foo(T&) in your snippet is a better match, as no const-qualifier is added.






    share|improve this answer





















    • 4





      Ah yes! Indeed the whole const vs non-const thing. +1

      – StoryTeller
      14 hours ago











    • I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

      – geza
      11 hours ago











    • Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

      – lubgr
      11 hours ago











    • @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

      – geza
      11 hours ago
















    9















    Why is the "non-template" requirement there in the text?




    Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:



    struct Foo {
    // ctor template: clearly useful and necessary
    template <typename T>
    Foo(const T&) {}

    // copy ctor: same signature! can't work
    template <typename T>
    Foo(const T &) {}
    };


    Besides, constructing a Foo from an object that is not a Foo can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).




    In this example, here will be printed. So it seems that my template constructor is a copy constructor




    The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to



    template <typename T>
    Foo(const T &) {
    // ^^^^^
    printf("heren");
    }


    then Foo b = a; results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:



    Foo(const Foo&);


    This requires adding a const-qualifier to a in Foo b = a;. The original constructor template Foo(T&) in your snippet is a better match, as no const-qualifier is added.






    share|improve this answer





















    • 4





      Ah yes! Indeed the whole const vs non-const thing. +1

      – StoryTeller
      14 hours ago











    • I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

      – geza
      11 hours ago











    • Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

      – lubgr
      11 hours ago











    • @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

      – geza
      11 hours ago














    9












    9








    9








    Why is the "non-template" requirement there in the text?




    Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:



    struct Foo {
    // ctor template: clearly useful and necessary
    template <typename T>
    Foo(const T&) {}

    // copy ctor: same signature! can't work
    template <typename T>
    Foo(const T &) {}
    };


    Besides, constructing a Foo from an object that is not a Foo can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).




    In this example, here will be printed. So it seems that my template constructor is a copy constructor




    The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to



    template <typename T>
    Foo(const T &) {
    // ^^^^^
    printf("heren");
    }


    then Foo b = a; results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:



    Foo(const Foo&);


    This requires adding a const-qualifier to a in Foo b = a;. The original constructor template Foo(T&) in your snippet is a better match, as no const-qualifier is added.






    share|improve this answer
















    Why is the "non-template" requirement there in the text?




    Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:



    struct Foo {
    // ctor template: clearly useful and necessary
    template <typename T>
    Foo(const T&) {}

    // copy ctor: same signature! can't work
    template <typename T>
    Foo(const T &) {}
    };


    Besides, constructing a Foo from an object that is not a Foo can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).




    In this example, here will be printed. So it seems that my template constructor is a copy constructor




    The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to



    template <typename T>
    Foo(const T &) {
    // ^^^^^
    printf("heren");
    }


    then Foo b = a; results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:



    Foo(const Foo&);


    This requires adding a const-qualifier to a in Foo b = a;. The original constructor template Foo(T&) in your snippet is a better match, as no const-qualifier is added.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 14 hours ago

























    answered 14 hours ago









    lubgrlubgr

    16.5k32557




    16.5k32557








    • 4





      Ah yes! Indeed the whole const vs non-const thing. +1

      – StoryTeller
      14 hours ago











    • I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

      – geza
      11 hours ago











    • Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

      – lubgr
      11 hours ago











    • @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

      – geza
      11 hours ago














    • 4





      Ah yes! Indeed the whole const vs non-const thing. +1

      – StoryTeller
      14 hours ago











    • I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

      – geza
      11 hours ago











    • Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

      – lubgr
      11 hours ago











    • @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

      – geza
      11 hours ago








    4




    4





    Ah yes! Indeed the whole const vs non-const thing. +1

    – StoryTeller
    14 hours ago





    Ah yes! Indeed the whole const vs non-const thing. +1

    – StoryTeller
    14 hours ago













    I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

    – geza
    11 hours ago





    I don't understand the first part of your answer. Did you intend to put template for both constructors? What I considered logical is that if a constructor is specialized with T=Foo, then it is a copy constructor.

    – geza
    11 hours ago













    Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

    – lubgr
    11 hours ago





    Both template were intended, I tried to make the point that if you have an ordinary ctor template (because it's useful), but you also need to implement copy constructor for your class (because it manages a resource), they can't co-exist, but they also can't be one entity, as they serve a different purpose. Admittedly, I see your point though, but it is realistic scenario that you have a general-purpose ctor template that does the right thing when instantiated with Foo? I you'd need to specialize it, this nothing but the non-template copy ctor, right?

    – lubgr
    11 hours ago













    @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

    – geza
    11 hours ago





    @lubgr: Sorry, I meant "instantiated with T=Foo" (so not explicit specialization). I got confused with these terms lately after reading an answer here in SO. I think I need to read the standard about this (I remembered that what we call instantiation, the standard calls implicit(ly instantiated) specialization).

    – geza
    11 hours ago











    0














    A copy constructor is of the form X(X& ) or (X const&), and it will be provided for you by the compiler if you didn't declare one yourself. Non-template comes here probably due to issues if you use template classes.



    Let's say there is a template class that has a template copy constructor. The problem is that when you instantiate that class using another instance of this class with the same template type, your template copy constructor will not be called.



    The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution.



    Source: C++ template copy constructor on template class






    share|improve this answer










    New contributor




    BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      A copy constructor is of the form X(X& ) or (X const&), and it will be provided for you by the compiler if you didn't declare one yourself. Non-template comes here probably due to issues if you use template classes.



      Let's say there is a template class that has a template copy constructor. The problem is that when you instantiate that class using another instance of this class with the same template type, your template copy constructor will not be called.



      The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution.



      Source: C++ template copy constructor on template class






      share|improve this answer










      New contributor




      BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        A copy constructor is of the form X(X& ) or (X const&), and it will be provided for you by the compiler if you didn't declare one yourself. Non-template comes here probably due to issues if you use template classes.



        Let's say there is a template class that has a template copy constructor. The problem is that when you instantiate that class using another instance of this class with the same template type, your template copy constructor will not be called.



        The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution.



        Source: C++ template copy constructor on template class






        share|improve this answer










        New contributor




        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        A copy constructor is of the form X(X& ) or (X const&), and it will be provided for you by the compiler if you didn't declare one yourself. Non-template comes here probably due to issues if you use template classes.



        Let's say there is a template class that has a template copy constructor. The problem is that when you instantiate that class using another instance of this class with the same template type, your template copy constructor will not be called.



        The issue isn't that your copy constructor template doesn't match. The issue is that the implicit copy constructor is not a function template, and non-templates are preferred to template specializations when it comes to overload resolution.



        Source: C++ template copy constructor on template class







        share|improve this answer










        New contributor




        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited 9 hours ago









        Peter Mortensen

        14k1987114




        14k1987114






        New contributor




        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 14 hours ago









        BlackFurryBlackFurry

        11




        11




        New contributor




        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        BlackFurry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55845896%2fwhy-doesnt-the-standard-consider-a-template-constructor-as-a-copy-constructor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List of shipwrecks in 1808...

            Is there a lightweight tool to crop images quickly?Cropping Images using Command Line Tools OnlyHow to crop...

            Unit packagekit.service is masked Announcing the arrival of Valued Associate #679: Cesar...