fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<int I,class T>
  5. struct static_case {
  6. static constexpr int value = I;
  7. using type = T;
  8. };
  9.  
  10.  
  11. template<int I, class DefaultType, class Case1, class... OtherCases>
  12. struct static_switch{
  13. using type = typename std::conditional< I==Case1::value ,
  14. typename Case1::type,
  15. typename static_switch<I,DefaultType,OtherCases...>::type
  16. >::type;
  17. };
  18.  
  19.  
  20. template<int I, class DefaultType, class LastCase>
  21. struct static_switch<I,DefaultType,LastCase> {
  22. using type = typename std::conditional< I==LastCase::value ,
  23. typename LastCase::type,
  24. DefaultType
  25. >::type;
  26. };
  27.  
  28.  
  29. template<class T>
  30. typename static_switch<sizeof(T)
  31. ,int // default case
  32. ,static_case<sizeof(char),char>
  33. ,static_case<sizeof(short),short>
  34. ,static_case<sizeof(long),long>
  35. ,static_case<sizeof(long long),long long>
  36. >::type foo(T bar){ return reinterpret_cast<decltype(foo(bar))&>(bar);}
  37.  
  38.  
  39. auto val = foo(13.0);
  40. static_assert(std::is_same<long long, decltype(val)>::value,"Not converted to long long!");
  41.  
  42. using namespace std;
  43.  
  44. int main() {
  45. // your code goes here
  46. return 0;
  47. }
Success #stdin #stdout 0s 3092KB
stdin
Standard input is empty
stdout
Standard output is empty