I am trying to add a struct with a const member& to a priority_queue.
Here's a minimal example of it:
#include <queue>
struct A
{
A(const int& i)
: m_i(i)
{
}
bool operator<(const A& other) const
{
return m_i < other.m_i;
}
const int& m_i;
};
int main()
{
std::priority_queue<A> q;
int i = 3;
const int& f = i;
q.emplace(f);
return 0;
}
I understand the error for this
Error 2 error C2582: 'operator =' function is unavailable in 'A' c:...\algorithm 2322
My A has no operator= overloaded, since it can't because of the const int& i.
I there a way to make this work?
The member needs to be a reference and only const functions can be called on this reference but I don't mind if the value in the struct is changed.
I tried using int& const m_i and implementing the operator= but then I can't initialize the emplace with a const int argument (and I probably don't understand enough what a & const is vs a const&).
Aucun commentaire:
Enregistrer un commentaire