fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. struct one {
  8. void foo(const int) {}
  9. void bar() {}
  10. };
  11.  
  12. struct two {
  13. void foo(const int) {}
  14. };
  15.  
  16. struct three {
  17. void foo(const int) {}
  18. void bar() {}
  19. };
  20.  
  21. struct owner {
  22. map<int, one> ones;
  23. map<int, two> twos;
  24. map<int, three> threes;
  25.  
  26. template <typename T, typename Func>
  27. void callFunc(T& param, const Func& func) {
  28. func(param);
  29. }
  30.  
  31. template <typename T>
  32. void findObject(int key, const T& func) {
  33. if(ones.count(key) != 0U) {
  34. callFunc(ones[key], func);
  35. } else if(twos.count(key) != 0U) {
  36. callFunc(twos[key], func);
  37. } else {
  38. callFunc(threes[key], func);
  39. }
  40. }
  41.  
  42. void foo(const int key, const int param) { findObject(key, [&](auto& value) { value.foo(param); } ); }
  43. void bar(const int key) { findObject(key, [&](auto& value) { value.bar(); } ); }
  44. };
  45. int main() {
  46. owner myOwner;
  47.  
  48. myOwner.ones.insert(make_pair(0, one()));
  49. myOwner.twos.insert(make_pair(1, two()));
  50. myOwner.threes.insert(make_pair(2, three()));
  51.  
  52. myOwner.foo(2, 1);
  53. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘owner::bar(int)::<lambda(auto:2&)> [with auto:2 = two]’:
prog.cpp:28:13:   required from ‘void owner::callFunc(T&, const Func&) [with T = two; Func = owner::bar(int)::<lambda(auto:2&)>]’
prog.cpp:36:13:   required from ‘void owner::findObject(int, const T&) [with T = owner::bar(int)::<lambda(auto:2&)>]’
prog.cpp:43:81:   required from here
prog.cpp:43:72: error: ‘struct two’ has no member named ‘bar’
     void bar(const int key) { findObject(key, [&](auto& value) { value.bar(); } ); }
                                                                  ~~~~~~^~~
stdout
Standard output is empty