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

后端 未结 3 1847
你的背包
你的背包 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:50

    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.

提交回复
热议问题