Why can't I initialize an array of objects if they have private copy constructors?

前端 未结 4 381
夕颜
夕颜 2021-01-24 08:34

I just ran across some unexpected and frustrating behaviour while working on a C++ project. My actual code is a tad more complicated, but the following example captures it just

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 08:56

    Because individual array elements are initialized by copy-initialization from the initializers specified through = {...} syntax. See 8.5/12 (C++03)

    The initialization that occurs in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and brace-enclosed initializer lists (8.5.1) is called copy-initialization

    Copy-initialization requires copy constructor (even if it won't actually use it).

    In practice, if you make your code compile by making the copy constructor public, the compiler will probably end up initializing your array elements in place, without using the copy constructor. Nevertheless, the formal rules of abstract language call for copy-initialization in this context.

提交回复
热议问题