fork download
  1. #include <string>
  2.  
  3. int lua_getnumber() { return 42; }
  4. std::string lua_getstring() { return "foo"; }
  5.  
  6. #include <type_traits>
  7.  
  8. template <typename T>
  9. using EnableIf = typename std::enable_if<T::value, int>::type;
  10. template <typename T>
  11. using DisableIf = typename std::enable_if<not T::value, int>::type;
  12.  
  13. namespace lua {
  14. template <typename T,
  15. EnableIf<std::is_arithmetic<T>>...>
  16. T get() { return lua_getnumber(); }
  17.  
  18. template <typename T,
  19. DisableIf<std::is_arithmetic<T>>...>
  20. T get() { return lua_getstring(); }
  21. }
  22.  
  23. #include <iostream>
  24.  
  25. int main() {
  26. std::cout << lua::get<int>() << '\n';
  27. std::cout << lua::get<double>() << '\n';
  28. std::cout << lua::get<std::string>() << '\n';
  29. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
42
42
foo