fork download
  1. #include <tuple>
  2.  
  3. template <class... T>
  4. struct input
  5. {
  6. std::tuple<T...> var;
  7. input(T&&... t) : var(std::forward<T>(t)...) {}
  8.  
  9. template <
  10. std::size_t N,
  11. typename std::enable_if<(N < std::tuple_size<decltype(var)>::value)>::type* = nullptr
  12. >
  13. auto get()
  14. -> typename std::tuple_element<N, decltype(var)>::type&&
  15. {
  16. return std::move( std::get<N>(var) );
  17. }
  18.  
  19. template <
  20. std::size_t N,
  21. typename std::enable_if<(N >= std::tuple_size<decltype(var)>::value)>::type* = nullptr
  22. >
  23. void get()
  24. {
  25. static_assert(N < std::tuple_size<decltype(var)>::value, "Out of bounds!");
  26. }
  27. };
  28.  
  29. template <class... Args>
  30. void f(Args&&... args)
  31. {
  32. auto arguments = input<Args...>(std::forward<Args>(args)...);
  33.  
  34. arguments.template get<9>(); // returns 2 but I'd rather throw an exception
  35. }
  36.  
  37. int main()
  38. {
  39. f(2, 4, 6, 8);
  40. }
  41.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void input<T>::get() [with unsigned int N = 9u; typename std::enable_if<(N >= std::tuple_size<decltype (((input<T>*)0)->input<T>::var)>::value), void>::type* <anonymous> = 0u; T = {int, int, int, int}]’:
prog.cpp:34:5:   required from ‘void f(Args&& ...) [with Args = {int, int, int, int}]’
prog.cpp:39:17:   required from here
prog.cpp:25:10: error: static assertion failed: Out of bounds!
stdout
Standard output is empty