Dukales

global functor on demand

Sep 25th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <utility>
  4.  
  5. #include <cstdlib>
  6.  
  7. #pragma clang diagnostic push
  8. #pragma clang diagnostic ignored "-Wglobal-constructors"
  9. template< typename F >
  10. F callee = {};
  11. template< typename F >
  12. auto call = [] (auto &&... args) -> decltype(auto) { return std::forward< F >(callee< std::decay_t< F > >)(std::forward< decltype(args) >(args)...); };
  13. #pragma clang diagnostic pop
  14.  
  15. #define PP { std::cout << ++i << ' ' << __PRETTY_FUNCTION__ << std::endl; }
  16.  
  17. struct L
  18. {
  19.     mutable int i = 0;
  20.     void operator () () const & { PP }
  21.     void operator () () & { PP }
  22.     void operator () () const && { PP }
  23.     void operator () () && { PP }
  24.     void operator () () volatile const & { PP }
  25.     void operator () () volatile & { PP }
  26.     void operator () () volatile const && { PP }
  27.     void operator () () volatile && { PP }
  28. };
  29.  
  30. struct M
  31. {
  32.     mutable int i = 0;
  33.     void operator () () const { PP }
  34.     void operator () () { PP }
  35.     void operator () () volatile const { PP }
  36.     void operator () () volatile { PP }
  37. };
  38.  
  39. int
  40. main()
  41. {
  42.     { auto const & c = call< L >; c(); }
  43.     { auto const & c = call< L & >; c(); }
  44.     { auto const & c = call< L && >; c(); }
  45.     { auto const & c = call< L const >; c(); }
  46.     { auto const & c = call< L const & >; c(); }
  47.     { auto const & c = call< L const && >; c(); }
  48.     { auto const & c = call< volatile L const >; c(); }
  49.     { auto const & c = call< volatile L const & >; c(); }
  50.     { auto const & c = call< volatile L const && >; c(); }
  51.     std::cout << std::endl;
  52.     { auto const & c = call< M >; c(); }
  53.     { auto const & c = call< M & >; c(); }
  54.     { auto const & c = call< M && >; c(); }
  55.     { auto const & c = call< M const >; c(); }
  56.     { auto const & c = call< M const & >; c(); }
  57.     { auto const & c = call< M const && >; c(); }
  58.     { auto const & c = call< volatile M const >; c(); }
  59.     { auto const & c = call< volatile M const & >; c(); }
  60.     { auto const & c = call< volatile M const && >; c(); }
  61.     return EXIT_SUCCESS;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment