Avoid calling move constructor

  • A+
Category:Languages

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?

 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: