fork download
  1. #include <cstddef>
  2.  
  3. template <typename T, unsigned int index, T In, T... args>
  4. struct GetArg
  5. {
  6. static const T value = GetArg<T, index-1, args...>::value;
  7. };
  8.  
  9. template <typename T, T In, T... args>
  10. struct GetArg<T, 0, In, args...>
  11. {
  12. static const T value = In;
  13. };
  14.  
  15. template <typename T, T In>
  16. struct GetArg<T, 1, In>
  17. {
  18. static const T value = -1;
  19. };
  20.  
  21. template <typename T, std::size_t Size>
  22. struct CVector
  23. {
  24. };
  25.  
  26. template <typename T, std::size_t Height, std::size_t Width>
  27. struct CMatrix
  28. {
  29. };
  30.  
  31. template <typename T, std::size_t... Sizes>
  32. struct CTensor
  33. {
  34. template <std::size_t SZ = sizeof...(Sizes)>
  35. operator CVector<T, GetArg<std::size_t, 0, Sizes...>::value>() const
  36. {
  37. static_assert(SZ == 1, "You can only convert a rank 1 tensor to a vector");
  38. CVector<T, Sizes...> vecResult;
  39. return vecResult;
  40. }
  41.  
  42. template <std::size_t SZ = sizeof...(Sizes)>
  43. operator CMatrix<T, GetArg<std::size_t, 0, Sizes...>::value, GetArg<std::size_t, 1, Sizes...>::value>() const
  44. {
  45. static_assert(SZ == 2, "You can only convert a rank 2 tensor to a matrix");
  46. CMatrix<T, Sizes...> matResult;
  47. return matResult;
  48. }
  49. };
  50.  
  51. int main()
  52. {
  53. CTensor<float, 3> tensor3;
  54. CTensor<float, 3, 3> tensor3_3;
  55. CTensor<float, 3, 3, 3> tensor3_3_3;
  56. CVector<float, 3> vec(tensor3);
  57. //CVector<float, 3> vec2(tensor3_3);
  58. CMatrix<float, 3, 3> mat(tensor3_3);
  59. //CMatrix<float, 3, 3> mat2(tensor3_3_3);
  60. }
  61.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty