Does Visual Studio 2017 need an explicit move constructor declaration?

后端 未结 4 1303
南笙
南笙 2020-12-15 08:29

The below code can be compiled successfully using Visual Studio 2015, but it failed using Visual Studio 2017. Visual Studio 2017 reports:

error C2280:

4条回答
  •  我在风中等你
    2020-12-15 09:01

    When you declare a move constructor, the implicitly declared copy constructor is defined as deleted. On the other hand, when you don't declare a move constructor, the compiler implicitly defines the copy constructor when it need it. And this implicit definition is ill-formed.

    unique_ptr is not CopyInsertable in a container that uses a standard allocator because it is not copy constructible so the copy constructor of map_ is ill-formed (it could have been declared as deleted, but this is not required by the standard).

    As your example code show us, with newer version of MSVC, this ill-formed definition is generated with this example code. I do not think there is something in the standard that forbids it (even if this is realy surprising).

    So you should indeed ensure that the copy constructor of Node is declared or implicitly defined as deleted.

提交回复
热议问题