Avoid calling move constructor

吃可爱长大的小学妹 提交于 2020-01-12 13:48:08

问题


I have following example

#include <cstdint>

class FooC
{
public:
   FooC(uint16_t iPort, uint16_t iPin)
   : PORT(iPort)
   , PIN(iPin)
   {
   };

   ~FooC() = default;

   FooC() = delete;
   FooC(const FooC&) = delete;
   FooC(FooC&&) = delete;

private:
   const uint16_t PORT;
   const uint16_t PIN;
};

int main()
{
    FooC array[2] = {
       FooC(1,2),
       FooC(3,4)
    };
}

and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++11)

: In function 'int main()':

:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'

 };

 ^

:16:4: note: declared here

FooC(FooC&&) = delete;

^~~~

:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'

 };

 ^

:16:4: note: declared here

FooC(FooC&&) = delete;

^~~~

Compiler returned: 1

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


回答1:


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").




回答2:


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.



来源:https://stackoverflow.com/questions/53381152/avoid-calling-move-constructor

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