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?
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.
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.
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)
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With