copy-list-initialization of non-copyable types

后端 未结 2 640
清歌不尽
清歌不尽 2021-02-20 10:45

12.6.1 - Explicit initialization

struct complex {
  complex();
  complex(double);
  complex(double,double);
};

complex sqrt(complex,complex);

         


        
2条回答
  •  终归单人心
    2021-02-20 11:28

    The constructor-from-T is not explicit, and copy-list-initialization is not the same as copy-initialization. Both cause "constructors to be considered", but copy-initialization always "considers" the copy con­struc­tor, while list-initialization considers constructors with the list elements filled in (plus some details). To wit:

    struct Foo
    {
        Foo(int) {}
        Foo(Foo const &) = delete;
    };
    
    int main()
    {
        Foo f = { 1 };  // Fine
    }
    

    (This would fail if the constructor was explicit. Also, Foo x = 1; will of course fail on account of the deleted copy constructor.)

    Perhaps an even more enlightening use case:

    Foo make() { return { 2 }; }
    
    void take(Foo const &);
    take(make());
    

    Everything necessary for this is in 8.5.4/3 and in 13.3.1.7/1.

提交回复
热议问题