Change member-typedef depending on template parameter?

c++, c-preprocessor, templates

Solution

INTRODUCTION

Since you'd like `S<N>::Matrix` to yield a different type depending on the N passed, you will need to use some sort of meta template programming. The question is currently tagged with preprocessor, and the snippet explicitly tries to use it; but that is of little to no use in this case.

When the code is being preprocessed `N` is nothing more than a name, it hasn't got a value; yet.

Solution

The description mentiones if, if ... else, and else; and we are dealing with types.. looking through `<type_traits>` it seems like `std::conditional` would be a perfect match!

std::conditional<condition, type-if-true, type-if-false>::type;

Note: Depending on whether the expression found in `condition` yields true, or false, `::type` will be a typedef for either type-if-true, or type-if-false.

Let's write a sample implementation:

#include <type_traits>
template <int N>
class Solver
{
  public:
    typedef typename std::conditional<
      /*    */ (N <= 24),
      /* y? */ MyMatrix<float>,
      /* n? */ typename std::conditional<(N <= 53), MyMatrix<double>, MyMatrix<mpreal>>::type
    >::type matrix_type;

  ...
};
int main () {
  Solver<53>::matrix_type a; // Matrix<double>
  Solver<10>::matrix_type b; // Matrix<float>
  Solver<99>::matrix_type c; // Matrix<mpreal>
}

Problem

I have this problem here that I can’t figure out how to solve. I want a template class that takes an integer as template parameter and sets the template parameters for another class accordingly: ``` template <int T> class Solver { public: #if T <= 24 typedef MyMatrix<float> Matrix; #else if T <= 53 typedef MyMatrix<double> Matrix; #else typedef MyMatrix<mpreal> Matrix; #endif Matrix create(); }; ``` And then calling it like this: ``` Solver<53>::Matrix m = Solver<53>::create(); ``` How can I do something like this? At the moment with the code above, the compiler complaints that it doesn't know "Matrix", so I'm not sure if you can use the preprocessor on template parameters.

Original source

Related problems