Dukales

static_if

Nov 6th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. #include <cstdlib>
  5.  
  6. template< bool condition >
  7. void
  8. f(char const * message = "")
  9. {
  10.     auto a = [&] (std::true_type) { std::cout << __PRETTY_FUNCTION__ << ' ' << message << std::endl; };
  11.     auto b = [&] (std::false_type) { std::cout << __PRETTY_FUNCTION__ << ' ' << message << std::endl; };
  12.     struct static_if : decltype(a), decltype(b)
  13.     {
  14.         using decltype(a)::operator ();
  15.         using decltype(b)::operator ();
  16.         static_if(decltype(a) _a, decltype(b) _b)
  17.             : decltype(a){_a}, decltype(b){_b}
  18.         { ; }
  19.     };
  20.     static_if{a, b}(std::integral_constant< bool, condition >{});
  21. }
  22.  
  23. int
  24. main()
  25. {
  26.     f< true >("Success!");
  27.     f< false >("Failure!");
  28.     return EXIT_SUCCESS;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment