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

后端 未结 3 956
醉话见心
醉话见心 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:04

    The class is an aggregate, so list-initialisation will perform aggregate initialisation, and won't consider the implicitly-declared constructors.

    Since there are no data members, only an empty list can be a valid aggregate initialiser.

    But when I use A b(a) instead of A b{a} all compiles correctly.

    Direct initialisation will use the implicit constructor rather than attempting aggregate initialisation.

    And if I declare default constructor it compiles too.

    By declaring a constructor, the class is no longer an aggregate, and can only be initialised using a constructor.

提交回复
热议问题