Avoid calling move constructor

喜夏-厌秋 提交于 2019-12-04 01:53:46

In C++11 and C++14, you can use nested braces:

FooC array[2] = {{1,2}, {3,4}};

In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").

Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?

No with your current syntax (before C++17) and yes (in C++17).

Pre-C++17:

This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.

FooC array[2] = {
   {1, 2},
   {3, 4}
};

C++17:

You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!