C++11 in gcc 4.8.1: list-initialization for copy constructor doesn't work

后端 未结 3 962
醉话见心
醉话见心 2021-01-02 07:33

I encourages with this problem: If I have

class A
{
public:
};
int main()
{
   A a{};
   A b{a};
}

gcc gives:

moves.

3条回答
  •  庸人自扰
    2021-01-02 08:00

    When not defining your own constructors class A will be considered an aggregate (ie. plain-old-data) storage type.

    When dealing with an aggregate, list-initialization won't consider any implicitly declared constructors, instead it will try to initialize the members of your object directly.

    In the case of A b { a } where A is an aggregate the compiler will try to initialize the first member in A with the value of a; this will of course fail since A doesn't contain any members.


    What does the standard say?

    [8.5.1 Aggregates]

    1) An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializer s for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

    2) When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer are taken as initializers for the members of the aggregate, in increasing subscript or member order. Each member is copy-initialized from the corresponding initializer-clause . If the initializer-clause is an expression and a narrowing conversion (8.5.4) is required to convert the expression, the program is ill-formed.

提交回复
热议问题