fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <utility>
  4.  
  5. #define PP_CAT(a, b) PP_CAT_I(a, b)
  6. #define PP_CAT_I(a, b) a ## b
  7.  
  8. template<typename F>
  9. struct scope_exit_t {
  10. scope_exit_t(F & f) : f(f) {}
  11. ~scope_exit_t() { std::cout << "by lvalue: "; f(); }
  12. private:
  13. F & f;
  14. };
  15.  
  16. template<typename F>
  17. struct scope_exit_t<F&&> {
  18. scope_exit_t(F && f) : f(f) {}
  19. ~scope_exit_t() { std::cout << "by rvalue: "; f(); }
  20. private:
  21. F f;
  22. };
  23.  
  24. struct scope_exit_helper {
  25. template<typename F>
  26. scope_exit_t<F&&> operator->*(F && f) const {
  27. return scope_exit_t<F&&>(std::forward<F>(f));
  28. }
  29. };
  30.  
  31. #define scope_exit_base auto PP_CAT(scope_exit_, __LINE__) = scope_exit_helper() ->*
  32. #define scope_exit scope_exit_base [&] ()
  33. #define scope_exit_2 scope_exit_base
  34.  
  35. struct fun_obj {
  36. void operator()() { std::cout << "fugafuga\n"; }
  37. };
  38.  
  39. void f() {
  40. std::cout << "私はカモメ\n";
  41. }
  42.  
  43. void g(int x) {
  44. std::cout << "x\n";
  45. }
  46.  
  47. int main() {
  48. scope_exit { std::cout << "hogehoge\n"; };
  49. scope_exit { std::cout << "piyopiyo\n"; };
  50. scope_exit_2 f;
  51. fun_obj x;
  52. scope_exit_2 x;
  53. scope_exit_2 std::bind(g, 10);
  54. }
  55.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:17: error: template argument 1 is invalid
prog.cpp:26: error: template argument 1 is invalid
prog.cpp:26: error: expected ‘,’ or ‘...’ before ‘&&’ token
prog.cpp: In member function ‘int scope_exit_helper::operator->*(F) const’:
prog.cpp:27: error: template argument 1 is invalid
prog.cpp:27: error: ‘forward’ is not a member of ‘std’
prog.cpp:27: error: ‘f’ was not declared in this scope
prog.cpp:27: error: ‘forward’ is not a member of ‘std’
prog.cpp:27: error: expected primary-expression before ‘>’ token
prog.cpp: In function ‘int main()’:
prog.cpp:48: error: ISO C++ forbids declaration of ‘scope_exit_48’ with no type
prog.cpp:48: error: expected primary-expression before ‘[’ token
prog.cpp:48: error: expected primary-expression before ‘]’ token
prog.cpp:48: error: expected ‘,’ or ‘;’ before ‘{’ token
prog.cpp:49: error: ISO C++ forbids declaration of ‘scope_exit_49’ with no type
prog.cpp:49: error: expected primary-expression before ‘[’ token
prog.cpp:49: error: expected primary-expression before ‘]’ token
prog.cpp:49: error: expected ‘,’ or ‘;’ before ‘{’ token
prog.cpp:50: error: ISO C++ forbids declaration of ‘scope_exit_50’ with no type
prog.cpp:52: error: ISO C++ forbids declaration of ‘scope_exit_52’ with no type
prog.cpp:53: error: ISO C++ forbids declaration of ‘scope_exit_53’ with no type
prog.cpp:53: error: ‘bind’ is not a member of ‘std’
prog.cpp:48: warning: unused variable ‘scope_exit_48’
prog.cpp:49: warning: unused variable ‘scope_exit_49’
prog.cpp:50: warning: unused variable ‘scope_exit_50’
prog.cpp:52: warning: unused variable ‘scope_exit_52’
prog.cpp:53: warning: unused variable ‘scope_exit_53’
stdout
Standard output is empty