I am stuck with an old compiler that does neither support std::move nor other c++11 features and have the following scenario:
struct MyStruct
{
const int32_t value1;
const int32_t value2;
MyStruct(int32_t value1, int32_t value2); //Initializing constructor.
}
Now I have a std::vector, which takes said structure, but my compiler kindly informs me: non-static const member ... can't use default assignment operator
.
This, in turn, suggests, that the std::vector does, for some reason, not use the copy constructor, which would be the right thing to do.
Any idea how to get around this?
Because
MyStruct &MyStruct::operator =(const MyStruct &other)
{
if (&other != this)
*this = MyStruct(other);
return *this;
}
feels like a very bug-prone approach to the issue.
An alternative thought to the same end would look like this:
MyStruct &MyStruct::operator =(const MyStruct &other)
{
*const_cast<int32_t *>(&value1) = other.value1;
*const_cast<int32_t *>(&value2) = other.value2;
return *this;
}
And I’m really not sure, which approach I like less.
1