fork download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <typeinfo>
  4. using namespace std;
  5.  
  6. template <typename PRECISION, int R, int C>
  7. struct Matrix
  8. {
  9. Matrix() {}
  10. };
  11.  
  12. template <typename DERIVED>
  13. struct VariableTraits
  14. {
  15. // static const int DIM;
  16. };
  17.  
  18. template <typename DERIVED>
  19. struct WrappedTraits
  20. {
  21. // static const int DIM;
  22. // typedef std::tuple<VARIABLE0, VARIABLE1, ...> VARIABLES;
  23. };
  24.  
  25. template <int N, typename WRAPPER>
  26. struct WrappedType
  27. {
  28. typedef WrappedTraits<WRAPPER> Traits;
  29. static const int DIM = Traits::DIM;
  30. typedef typename Traits::VARIABLES VARIABLES;
  31. typedef Matrix<float, Traits::DIM, VariableTraits<typename std::tuple_element<N, VARIABLES>::type>::DIM> type;
  32. };
  33.  
  34. template<int N, typename WRAPPER>
  35. struct MatrixTypeImpl
  36. {
  37. typedef std::tuple<typename MatrixTypeImpl<N - 1, WRAPPER>::type, typename WrappedType<N, WRAPPER>::type> type;
  38. };
  39.  
  40. template <typename WRAPPER>
  41. struct MatrixTypeImpl<0, WRAPPER>
  42. {
  43. typedef typename WrappedType<0, WRAPPER>::type type;
  44. };
  45.  
  46. template<typename WRAPPER>
  47. struct MatrixType
  48. {
  49. typedef WrappedTraits<WRAPPER> Traits;
  50. typedef typename MatrixTypeImpl<std::tuple_size<typename Traits::VARIABLES>::value - 1, WRAPPER>::type type;
  51. };
  52.  
  53. struct FooVariable
  54. {
  55. };
  56.  
  57. struct BarVariable
  58. {
  59. };
  60.  
  61. template <>
  62. struct VariableTraits<FooVariable>
  63. {
  64. static const int DIM = 2;
  65. };
  66.  
  67. template <>
  68. struct VariableTraits<BarVariable>
  69. {
  70. static const int DIM = 3;
  71. };
  72.  
  73. struct Wrapper
  74. {
  75. };
  76.  
  77. template <>
  78. struct WrappedTraits<Wrapper>
  79. {
  80. static const int DIM = 2;
  81. typedef std::tuple<FooVariable, BarVariable> VARIABLES;
  82. };
  83.  
  84. template <int N, typename WRAPPER>
  85. void hello(const typename std::tuple_element<N, typename MatrixType<WRAPPER>::type>::type& mat)
  86. {
  87.  
  88. }
  89.  
  90. int main()
  91. {
  92. typedef typename MatrixType<Wrapper>::type MatrixTuple;
  93.  
  94. Matrix<float, 2, 2> FooMatrix;
  95. Matrix<float, 2, 3> BarMatrix;
  96. // typedef typename std::tuple_element<0, MatrixTuple>::type FooMatrixType;
  97. // typedef typename std::tuple_element<1, MatrixTuple>::type BarMatrixType;
  98. // std::cout << typeid(FooMatrixType).name() << std::endl;
  99. // std::cout << typeid(BarMatrixType).name() << std::endl;
  100.  
  101. // FooMatrixType FooMatrix;
  102. // BarMatrixType BarMatrix;
  103.  
  104. hello<0, Wrapper>(FooMatrix);
  105. hello<1, Wrapper>(BarMatrix);
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0s 3136KB
stdin
Standard input is empty
stdout
Standard output is empty