fork(1) download
  1. #include <functional>
  2. #include <iostream>
  3. #include <map>
  4. #include <memory>
  5.  
  6. enum class EventType {
  7. SOME_EVENT = 0,
  8. };
  9.  
  10. class Event {
  11. public:
  12. virtual ~Event() {}
  13. virtual EventType get_event_type() = 0;
  14. };
  15.  
  16. class SomeEvent: public Event {
  17. public:
  18. SomeEvent(): m_event_type(EventType::SOME_EVENT) {}
  19. void bar() { std::cout << "hello" << std::endl; }
  20. EventType get_event_type() { return this->m_event_type; }
  21.  
  22. public:
  23. EventType m_event_type;
  24. };
  25.  
  26.  
  27. typedef std::function<void(std::unique_ptr<Event>)> EventHandler;
  28.  
  29. std::map<EventType, EventHandler> event_handlers;
  30.  
  31. template <class T>
  32. void add_event_handler(EventType event_type, const std::function<void(std::unique_ptr<T>)> &event_handler) {
  33. event_handlers[event_type] = event_handler;
  34. }
  35.  
  36. void insert_event(std::unique_ptr<Event> event) {
  37. event_handlers[event->get_event_type()](std::move(event));
  38. }
  39.  
  40. int main(int argc, char **argv) {
  41. add_event_handler<SomeEvent>(EventType::SOME_EVENT, [](auto b) { b->bar(); });
  42. // later...
  43. insert_event(std::make_unique<SomeEvent>());
  44. }
  45.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void add_event_handler(EventType, const std::function<void(std::unique_ptr<_Tp>)>&) [with T = SomeEvent]’:
prog.cpp:41:79:   required from here
prog.cpp:33:30: error: no match for ‘operator=’ (operand types are ‘std::map<EventType, std::function<void(std::unique_ptr<Event>)> >::mapped_type’ {aka ‘std::function<void(std::unique_ptr<Event>)>’} and ‘const std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>’)
   event_handlers[event_type] = event_handler;
In file included from /usr/include/c++/8/functional:59,
                 from prog.cpp:1:
/usr/include/c++/8/bits/std_function.h:461:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(const std::function<_Res(_ArgTypes ...)>&) [with _Res = void; _ArgTypes = {std::unique_ptr<Event, std::default_delete<Event> >}]’
       operator=(const function& __x)
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:461:7: note:   no known conversion for argument 1 from ‘const std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>’ to ‘const std::function<void(std::unique_ptr<Event>)>&’
/usr/include/c++/8/bits/std_function.h:479:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::function<_Res(_ArgTypes ...)>&&) [with _Res = void; _ArgTypes = {std::unique_ptr<Event, std::default_delete<Event> >}]’
       operator=(function&& __x) noexcept
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:479:7: note:   no known conversion for argument 1 from ‘const std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>’ to ‘std::function<void(std::unique_ptr<Event>)>&&’
/usr/include/c++/8/bits/std_function.h:493:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::nullptr_t) [with _Res = void; _ArgTypes = {std::unique_ptr<Event, std::default_delete<Event> >}; std::nullptr_t = std::nullptr_t]’
       operator=(nullptr_t) noexcept
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:493:7: note:   no known conversion for argument 1 from ‘const std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>’ to ‘std::nullptr_t’
/usr/include/c++/8/bits/std_function.h:522:2: note: candidate: ‘template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_Callable<typename std::decay<_U1>::type>, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = _Functor; _Res = void; _ArgTypes = {std::unique_ptr<Event, std::default_delete<Event> >}]’
  operator=(_Functor&& __f)
  ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:522:2: note:   template argument deduction/substitution failed:
/usr/include/c++/8/bits/std_function.h: In substitution of ‘template<class _Functor> std::function<void(std::unique_ptr<Event>)>::_Requires<std::function<void(std::unique_ptr<Event>)>::_Callable<typename std::decay<_Tp>::type, typename std::result_of<typename std::decay<_Tp>::type&(std::unique_ptr<Event>)>::type>, std::function<void(std::unique_ptr<Event>)>&> std::function<void(std::unique_ptr<Event>)>::operator=<_Functor>(_Functor&&) [with _Functor = const std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>&]’:
prog.cpp:33:30:   required from ‘void add_event_handler(EventType, const std::function<void(std::unique_ptr<_Tp>)>&) [with T = SomeEvent]’
prog.cpp:41:79:   required from here
/usr/include/c++/8/bits/std_function.h:522:2: error: no type named ‘type’ in ‘class std::result_of<std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>&(std::unique_ptr<Event>)>’
prog.cpp: In instantiation of ‘void add_event_handler(EventType, const std::function<void(std::unique_ptr<_Tp>)>&) [with T = SomeEvent]’:
prog.cpp:41:79:   required from here
/usr/include/c++/8/bits/std_function.h:531:2: note: candidate: ‘template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Functor>) [with _Functor = _Functor; _Res = void; _ArgTypes = {std::unique_ptr<Event, std::default_delete<Event> >}]’
  operator=(reference_wrapper<_Functor> __f) noexcept
  ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:531:2: note:   template argument deduction/substitution failed:
prog.cpp:33:30: note:   ‘std::function<void(std::unique_ptr<SomeEvent, std::default_delete<SomeEvent> >)>’ is not derived from ‘std::reference_wrapper<_Tp>’
   event_handlers[event_type] = event_handler;
stdout
Standard output is empty