I am trying to insert a moveable, non-copyable object into std::vector
. I reduced the original code to this example:
#include <vector>
class MovingClass
{
public:
MovingClass(const int value) : _value(new int(value)) {}
//! No copy allowed
MovingClass(const MovingClass& src) = delete;
MovingClass& operator=(const MovingClass& src) = delete;
// move is OK
MovingClass(MovingClass&& source) : _value(source._value) { source._value = nullptr; }
MovingClass& operator=(MovingClass&& source) { _value = source._value; source._value = nullptr; }
~MovingClass() { delete _value; }
// do not mind that this is all kinds of bad, it's just an example
int* _value;
};
int test_vector_main()
{
std::vector<MovingClass> containers;
containers.push_back({ 42 });
return 0;
}
The original purpose of the class is closing winapi handles, not deleting pointers.
I can reproduce the warning in a blank project if I set "Enable C++ Exceptions" to "No" in Configuration Properties->C/C++->All Options
.
The project I work with treats warnings as errors and has exceptions disabled.
I am not sure what to do except giving up on no-copy rules or enabling the exceptions. I'll enable the exceptions, but if I couldn't what else could I do?
Why does push_back
even require exceptions?