lxr196
lxr196

Reputation: 108

Why is this struct's move asignement operator deleted?

When I create an array of this struct and try to erase the first 2 members, it generates a compiler error (Visual Studio 2019):

struct A {
    virtual void unimplemented() = 0;
};
enum B {
    a, b, c, d, e, f
};
struct AorB {
    const bool isA;
    union {
        A* a;
        B b;
    };
    AorB(A* aVal) :a(aVal), isA(true) {};
    AorB(B bVal) :b(bVal), isA(false) {};
};
int main() {
    std::vector<AorB> vec{ a,b,c };
    vec.erase(vec.begin(), vec.begin() + 1);
}

Error:

Error C2280 'AorB &AorB::operator =(const AorB &)': attempting to reference a deleted function  
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\xutility    3907    

I presume the move assignment operator is called when the vector rearranges it's elements but I don't see why it is deleted. I removed the union but it still produced this error.

Upvotes: 2

Views: 87

Answers (1)

user11422223
user11422223

Reputation:

Remove the const qualifier and change it to bool isA;. You cannot modify isA inside your structure if your essentially want it as a constant.

Upvotes: 1

Related Questions