Priority Queue Comparator for Constant Member Object

I am trying to implement a priority queue that uses an object that has a const member, which is used to prioritize the objects in the queue. Below is a stripped down version of what I'm using

#include <vector>
#include <queue>

class Event {
public:
    Event(float _time) : time(_time) {};
    const float time;
};

struct EventComp {
public:
    bool operator()(const Event& a, const Event& b) const {
      return a.time < b.time;
    }
};

class EventQueue {
private:
    std::priority_queue<Event, std::vector<Event>, EventComp> events;
};


int main(int argc, char *argv[])
{
    EventQueue q;
}

When I try to compile (using g ++ -std = C ++ 11), I get the following error messages, which I don’t quite understand.

g++ -std=c++11 events.cpp -o events
In file included from /usr/include/c++/4.8/queue:62:0,
                 from events.cpp:2:
/usr/include/c++/4.8/bits/stl_heap.h: In instantiation ofvoid std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Event*, std::vector<Event> >; _Distance = long int; _Tp = Event; _Compare = EventComp]’:
/usr/include/c++/4.8/bits/stl_heap.h:448:15:   required fromvoid std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Event*, std::vector<Event> >; _Compare = EventComp]’
/usr/include/c++/4.8/bits/stl_queue.h:411:48:   required from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&) [with _Tp = Event; _Sequence = std::vector<Event>; _Compare = EventComp]’
events.cpp:15:7:   required from here
/usr/include/c++/4.8/bits/stl_heap.h:315:29: error: use of deleted functionEvent& Event::operator=(Event&&)’
    *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
                             ^
events.cpp:4:7: note: ‘Event& Event::operator=(Event&&)’ is implicitly deleted because the default definition would be ill-formed:
 class Event {
       ^
events.cpp:4:7: error: non-static const member ‘const float Event::time’, can’t use default assignment operator
In file included from /usr/include/c++/4.8/queue:62:0,
                 from events.cpp:2:
/usr/include/c++/4.8/bits/stl_heap.h:321:29: error: use of deleted functionEvent& Event::operator=(Event&&)’
    *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
                             ^

I assume that for some part of the internal structure, priority_queuea move destination operator is needed to insert elements, but the member constdoes not seem to allow this operator to be defined. I tried using rule five, but it didn't seem to work. What do I need to add to class definitions to get this working?

edit: , const -, private --- , , , public const, , - ( ).

+4
1

std::priority_queue<Event, ...> Event MoveAssignable, std::make_heap ( ). Event MoveAssignable - const float time. const , .

, const , private - , public const, , - ( ).

, std:: lib, Event MoveAssignable. , . . const, , , .

/ , :

class Event {
public:
    Event(float _time) : time(_time) {};
    const float time;

    Event(Event&&) = default;
    Event& operator=(Event&&);  // Supply this
};

, ( ), , , time - . , ( , ++).


<aside>

POW , clang (, lib++). lib++:

priority_queue:

explicit priority_queue(const Compare& x = Compare(),
                        Container&& c = Container());

std::make_heap c. - . lib++ :

priority_queue();
explicit priority_queue(const Compare& x);
explicit priority_queue(const Compare& x, Container&& c);

std::make_heap. , lib++ value_type.

, . , , , explicit. , priority_queue .

</aside>

+7

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


All Articles