fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. int f(int, int) { return 1; }
  5. int g(int, int) { return 2; }
  6. void test(std::function<int(int, int)> const& arg)
  7. {
  8. std::cout << "test function: ";
  9. if (arg.target<std::plus<int>>())
  10. std::cout << "it is plus\n";
  11. if (arg.target<std::minus<int>>())
  12. std::cout << "it is minus\n";
  13.  
  14. int (*const* ptr)(int, int) = arg.target<int(*)(int, int)>();
  15. if (ptr && *ptr == f)
  16. std::cout << "it is the function f\n";
  17. if (ptr && *ptr == g)
  18. std::cout << "it is the function g\n";
  19. }
  20.  
  21. class foo {
  22.  
  23. public:
  24.  
  25. void some_function(int) {
  26.  
  27. }
  28.  
  29. foo() {
  30. const auto func = std::function<void(int)> { [this](int i) {
  31. this->some_function(i);
  32. }};
  33. _member = func;
  34.  
  35. std::cout << "hello " << _member.target<decltype(func)>() << std::endl;
  36. }
  37. std::function<void(int)> _member;
  38. };
  39. int main()
  40. {
  41. foo bar {};
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
hello 0