operator+,-,*,/ (std::complex)
(1) | ||
template< class T > std::complex<T> operator+( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator+( const std::complex<T>& lhs, |
(自 C++20) | |
(2) | ||
template< class T > std::complex<T> operator+( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator+( const std::complex<T>& lhs, |
(自 C++20) | |
(3) | ||
template< class T > std::complex<T> operator+( const T& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator+( const T& lhs, |
(自 C++20) | |
(4) | ||
template< class T > std::complex<T> operator-( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator-( const std::complex<T>& lhs, |
(自 C++20) | |
(5) | ||
template< class T > std::complex<T> operator-( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator-( const std::complex<T>& lhs, |
(自 C++20) | |
(6) | ||
template< class T > std::complex<T> operator-( const T& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator-( const T& lhs, |
(自 C++20) | |
(7) | ||
template< class T > std::complex<T> operator*( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator*( const std::complex<T>& lhs, |
(自 C++20) | |
(8) | ||
template< class T > std::complex<T> operator*( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator*( const std::complex<T>& lhs, |
(自 C++20) | |
(9) | ||
template< class T > std::complex<T> operator*( const T& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator*( const T& lhs, |
(自 C++20) | |
(10) | ||
template< class T > std::complex<T> operator/( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator/( const std::complex<T>& lhs, |
(自 C++20) | |
(11) | ||
template< class T > std::complex<T> operator/( const std::complex<T>& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator/( const std::complex<T>& lhs, |
(自 C++20) | |
(12) | ||
template< class T > std::complex<T> operator/( const T& lhs, |
(直到 C++20) | |
template< class T > constexpr std::complex<T> operator/( const T& lhs, |
(自 C++20) | |
实现复数算术和复数/标量混合算术的二元运算符。标量参数被视为实部等于参数、虚部设置为零的复数。
内容 |
[编辑] 参数
lhs, rhs | - | 参数:两个复数或一个复数和一个匹配类型的标量(float, double, long double) |
[编辑] 返回值
[编辑] 说明
由于 模板参数推断 不考虑隐式转换,因此这些运算符不能用于混合整数/复数运算。在所有情况下,标量必须与复数的底层类型相同。
GCC 标记 "-fcx-limited-range"(由 "-ffast-math" 包含)通过移除浮点数边缘情况的检查来更改复数乘除的行为。这会影响循环向量化。
[编辑] 示例
#include <complex> #include <iostream> int main() { std::complex<double> c2(2.0, 0.0); std::complex<double> ci(0.0, 1.0); std::cout << ci << " + " << c2 << " = " << ci + c2 << '\n' << ci << " * " << ci << " = " << ci * ci << '\n' << ci << " + " << c2 << " / " << ci << " = " << ci + c2 / ci << '\n' << 1 << " / " << ci << " = " << 1.0 / ci << '\n'; // std::cout << 1.0f / ci; // compile error // std::cout << 1 / ci; // compile error }
输出
(0,1) + (2,0) = (2,1) (0,1) * (0,1) = (-1,0) (0,1) + (2,0) / (0,1) = (0,-1) 1 / (0,1) = (0,-1)
[编辑] 参见
两个复数或一个复数和一个标量的复合赋值 (公有成员函数) | |
将一元运算符应用于复数 (函数模板) |