对于多项式方程求解器,将其模板化以使任何类型可用是很好的:
- template <class number,int degree>
- class PolynomialEquation
- {
- public:
- private:
- array<number,degree+1> myEquation;
- array<complex<number>,degree> equationResult;
- };
例如,这允许doubleinℝ用于输入,结果是std :: complex< double>在ℂ(我们知道从2级及以上,方程的解通常落入ℂ,例如:x ^ 2 1).
但是,等式的输入也可以是std :: complex.在这种情况下,myEquation的类型应该是复杂的,但equationResult不应该是std :: complex< complex< T>>,而只是类型T的正常复数.
问题:
当方程式与std :: complex一起提供时,如何使equationResult的类型成为std :: complex的子类型?
是否有std :: is_floating_point等同于std :: is_complex_number?
解决方法
您可以创建一个特征,例如:
- template <typename T>
- struct to_complex {
- using type = std::complex<T>;
- };
- template <typename T>
- struct to_complex<std::complex<T>> {
- using type = std::complex<T>;
- };
然后
- template <class number,degree+1> myEquation;
- array<typename to_complex<number>::type,degree> equationResult;
- };