SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template <size_t SIZE, size_t i, typename T>
  2. void toArray(T(&target)[SIZE], const T& t)
  3. {
  4.     target[i] = t;
  5. }
  6.  
  7. template <size_t SIZE, size_t i, typename T, typename... Rest>
  8. void toArray(T(&target)[SIZE], const T& t, const Rest&... rest)
  9. {
  10.     target[i] = t;
  11.     toArray<SIZE, i + 1, Rest...>(target, rest...);
  12. }
  13.  
  14. template <size_t I, typename T>
  15. struct vec
  16. {
  17.     union {
  18.         T elements[I];
  19.     };
  20.  
  21.     template <typename... Rest>
  22.     vec(const Rest& ... rest)
  23.     {
  24.         static_assert(sizeof...(Rest) == I, "A vec cannot be initialized with the wrong number of arguments!");
  25.  
  26.         T unpacked[I];
  27.         toArray<I, 0, Rest...>(unpacked, rest...);
  28.  
  29.         for (size_t i = 0; i < I; i++)
  30.             elements[i] = unpacked[i];
  31.     };
  32.      
  33.     template <size_t J, typename U, typename... Rest>
  34.     vec(const vec<J, U>& other, const Rest&... rest)
  35.     {
  36.         static_assert(J + sizeof...(Rest) >= I, "A vec cannot be initialized with the wrong number of arguments!");
  37.  
  38.         if constexpr (I < J || I == J) {
  39.             for (size_t i = 0; i < I; i++)
  40.                 elements[i] = other.elements[i];
  41.         } else {
  42.             for (size_t i = 0; i < J; i++)
  43.                 elements[i] = other.elements[i];
  44.  
  45.             T unpacked[sizeof...(Rest)];
  46.             toArray<sizeof...(Rest), 0, Rest...>(unpacked, rest...);
  47.  
  48.             for (size_t i = J; i < I; i++)
  49.                 elements[i] = unpacked[i - J];
  50.         }
  51.     };
  52.  
  53.     std::string toString()
  54.     {
  55.         std::string str = "[";
  56.  
  57.         for (size_t i = 0; i < I; i++)
  58.             str.append( std::to_string(elements[i]) + ((i != I - 1)?(", "):("")) );
  59.        
  60.         str.append("]\n");
  61.  
  62.         return str;
  63.     }
  64. };
  65.  
  66. using vec1f = vec<1, float>;
  67. using vec2f = vec<2, float>;
  68. using vec3f = vec<3, float>;
  69. using vec4f = vec<4, float>;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top