Compile the error using std :: deque :: erase with constant class members

I get a compilation error here, and I have no idea what is wrong with the code. I am using g ++ 4.9.2.

#include<iostream>
#include<deque>

using std::string;
using std::deque;

class Dummy {
public:
    virtual ~Dummy(){};
    Dummy():ID_("00") {};
private:

    const string ID_;
};

int main(){
    {
    deque <Dummy> waiter;
    waiter.push_back(Dummy());
    waiter.erase( waiter.begin() );
    }
    return 0;
}

Edit: I know that removing const eliminates the compilation error, but I have no idea why. Anyway, I need this constant.

+2
source share
2 answers

std :: deque :: erase expects the item type to be MoveAssignable :

Type Requirements

T must comply with MoveAssignable requirements.

And the class Dummyhas a const member const string ID_;, which makes it an unassigned default assignment operator.

ID_ , . .

Dummy& operator=(const Dummy&) { /* do nothing */ }

LIVE

+5

const, :

string ID_;

:

class Dummy {
public:
    virtual ~Dummy(){};
    Dummy() {};
private:

    static const string ID_;
};

const string Dummy::ID_ = "00";

const string.

0

Source: https://habr.com/ru/post/1617209/


All Articles