I want to create a std::array
of objects which are not movable and not copyable. Also the default constructor is deleted, there is just a constructor that takes a string argument.
#include <iostream>
#include <array>
class Foo {
public:
Foo() = delete; // delete default constructor
Foo(const Foo &obj) = delete; // delete copy constructor
Foo(Foo &&obj) = delete; // delete move constructor
Foo(std::string label) {
label_ = label;
std::cout << "Constructor " << std::endl;
}
~Foo() {
std::cout << "Destructor" << std::endl;
}
private:
std::string label_;
};
int main() {
std::array<Foo, 3> arr = {Foo("a"), Foo("b"), Foo("c")};
}
This is not accepted by the compiler, and I get an error like:
use of deleted function ‘Foo::Foo(Foo&&)’
If I explicitly implement a move-constructor like this:
Foo(Foo &&obj) {
label_ = obj.label_;
std::cout << "MOVE constructor" << std::endl;
}
everything compiles and runs, but from the output I see that the move-constructor is not used. So why does the compiler complain about the missing move-constructor, if it doesn't use it anyway? And how should I correctly initialize an array of non-copyable, non-movable objects?