fork 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. template<int I, class DefaultType, class Case1, class... OtherCases>
  11. struct static_switch{
  12. using type = typename std::conditional< I==Case1::value ,
  13. typename Case1::type,
  14. typename static_switch<I,DefaultType,OtherCases...>::type
  15. >::type;
  16. };
  17.  
  18. struct fail_on_default {};
  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. static_assert(!(std::is_same<type, fail_on_default>::value),
  28. "Default case reached in static_switch!");
  29. };
  30.  
  31.  
  32. template<class T>
  33. typename static_switch<sizeof(T)
  34. ,fail_on_default // default case
  35. ,static_case<sizeof(long long),long long>
  36. >::type foo(T bar){ return reinterpret_cast<decltype(foo(bar))&>(bar);}
  37.  
  38. struct test{
  39. int a,b,c;
  40. };
  41.  
  42. auto val = foo(13.0);
  43. //auto val = foo(test());
  44. static_assert(std::is_same<long long, decltype(val)>::value,"Not converted to long long!");
  45.  
  46. using namespace std;
  47.  
  48. int main(){
  49. // your code goes here
  50. return 0;
  51. }
Success #stdin #stdout 0s 3092KB
stdin
Standard input is empty
stdout
Standard output is empty