介绍
既然你愿意S<N>::Matrix
根据不同的情况产生不同的类型N通过,你将需要使用某种元模板编程。该问题当前标记为预处理器,并且代码片段显式地尝试使用它;但这在这种情况下几乎没有用处。
当代码被预处理时N
只是一个名称,它没有值;然而。
Solution
所提到的描述if, 如果别的, and else;我们正在处理类型.. 浏览<type_traits> http://en.cppreference.com/w/cpp/header/type_traits这好像是std::conditional http://en.cppreference.com/w/cpp/types/conditional将是完美的搭配!
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.
让我们编写一个示例实现:
#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>
}