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

后端 未结 3 1846
你的背包
你的背包 2021-02-05 02:03

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

3条回答
  •  别那么骄傲
    2021-02-05 02:37

    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

提交回复
热议问题