Does a templated constructor override the implicit copy constructor in C++?

前端 未结 3 657
梦毁少年i
梦毁少年i 2021-01-18 05:29

Does a templated constructor (such as the following) override the implicit copy constructor?

template 
struct Foo
{
    T data;

    // ...

          


        
3条回答
  •  终归单人心
    2021-01-18 06:16

    No, that is not a copy constructor. Section 12.8 ([class.copy]) of the Standard requires that:

    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.

    The compiler will still implicitly generate a defaulted one.

    You can make that explicit (requires C++11) by

    Foo(const Foo&) = default;
    

提交回复
热议问题