Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid calling move constructor

Tags:

c++

c++11

c++17

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?

like image 903
ge45mue Avatar asked Nov 19 '18 19:11

ge45mue


People also ask

Does STD move move constructor?

std::move is actually just a request to move and if the type of the object has not a move constructor/assign-operator defined or generated the move operation will fall back to a copy.

Are move constructor automatically generated?

If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.

Why should move constructors be Noexcept?

noexcept is nice for two reasons: The compiler can optimize a little better because it doesn't need to emit any code for unwinding a call stack in case of an exception, and. It leads to incredible performance differences at runtime for std::vector (and other containers, too)

How do I turn off move constructor?

To correct this, remove the move constructor completely. In the case of the class, once a copy constructor is present (user defined), the move is implicitly not generated anyway (move constructor and move assignment operator).


2 Answers

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

like image 185
Kerrek SB Avatar answered Sep 19 '22 15:09

Kerrek SB


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.

like image 22
NathanOliver Avatar answered Sep 21 '22 15:09

NathanOliver