SHARE
TWEET

Variadic templates for vector constructor

a guest Nov 22nd, 2013 345 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <type_traits>
  2. #include <utility>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T, typename... Args>
  7. class VecBase
  8. {
  9. public:
  10.     static constexpr std::size_t size = sizeof...(Args);
  11. private:
  12.     T v[size];
  13.  
  14. public:
  15.     VecBase(Args... x) : v{x...} {}
  16. };
  17.  
  18. template<typename T, int rem, typename... Args>
  19. class VecHelper
  20. {
  21. public:
  22.     typedef typename VecHelper<T, rem - 1, T, Args...>::type type;
  23. };
  24.  
  25. template<typename T, typename... Args>
  26. class VecHelper<T, 0, Args...>
  27. {
  28. public:
  29.     typedef VecBase<T, Args...> type;
  30. };
  31.  
  32. template<typename T, int N>
  33. using Vec = typename VecHelper<T, N>::type;
  34.  
  35. int main()
  36. {
  37.     Vec<float, 3> v(1.0f, 2.0, 3);
  38. }
RAW Paste Data
Top