c++ - 提供 `std::complex` 时如何避免嵌入 `T=std::complex<Q>` ?

标签 c++ templates c++11 complex-numbers

对于多项式方程求解器,最好将其模板化为任何可用类型:

template <class number, int degree>
class PolynomialEquation
{
public:

private:
    array<number, degree+1> myEquation;
    array<complex<number>, degree> equationResult;
};

例如,这允许 double in ℝ 用于输入,结果为 std::complex<double>在 ℂ 中(我们知道从 2 次及以上,方程的解通常落入 ℂ,例如:x^2+1 )。

但是,方程式的输入也可以是 std::complex .在这种情况下,myEquation 的类型应该很复杂,但是 equationResult不应该是 std::complex<complex<T>> , 但只是一个 T 类型的普通复数.

问题:

如何制作equationResult的类型成为 std::complex 的子类型当方程带有 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, int degree>
class PolynomialEquation
{
public:

private:
    array<number, degree+1> myEquation;
    array<typename to_complex<number>::type, degree> equationResult;
};

关于c++ - 提供 `std::complex` 时如何避免嵌入 `T=std::complex<Q>` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641394/

相关文章:

c++ - 如何获得类似 C++ 模板的行为但只允许实例化两种特定类型?

regex - 正则表达式语法的通用子集?或每个语法的综合特征表

c - C 中的 volatile int 是否与 C++0x 中的 std::atomic<int> 一样好?

c++ - C++中的数据框库

c++ - 使用 getline 的 csv 处理行为不明确

c++ - 在 XCode 4.6.2 上使用 cryptopp 静态库在 iOS 应用程序中抛出异常

c++ - initializer_list 和参数相关的查找

c++ - 等效 typedef 之间的算术运算

c++ - 映射可变参数模板参数

c++ - 使用模板类型名的指针初始化