template< class T > complex<T> operator+( const complex<T>& lhs, const complex<T>& rhs); | (1) | |
template< class T > complex<T> operator+( const complex<T>& lhs, const T& rhs); | (2) | |
template< class T > complex<T> operator+( const T& lhs, const complex<T>& rhs); | (3) | |
template< class T > complex<T> operator-( const complex<T>& lhs, const complex<T>& rhs); | (4) | |
template< class T > complex<T> operator-( const complex<T>& lhs, const T& rhs); | (5) | |
template< class T > complex<T> operator-( const T& lhs, const complex<T>& rhs); | (6) | |
template< class T > complex<T> operator*( const complex<T>& lhs, const complex<T>& rhs); | (7) | |
template< class T > complex<T> operator*( const complex<T>& lhs, const T& rhs); | (8) | |
template< class T > complex<T> operator*( const T& lhs, const complex<T>& rhs); | (9) | |
template< class T > complex<T> operator/( const complex<T>& lhs, const complex<T>& rhs); | (10) | |
template< class T > complex<T> operator/( const complex<T>& lhs, const T& rhs); | (11) | |
template< class T > complex<T> operator/( const T& lhs, const complex<T>& rhs); | (12) |
Implements the binary operators for complex arithmetic and for mixed complex/scalar arithmetic. Scalar arguments are treated as complex numbers with the real part equal to the argument and the imaginary part set to zero.
rhs
from lhs
lhs
by rhs
lhs, rhs | - | the arguments: either both complex numbers or one complex and one scalar of matching type (float , double , long double ) |
complex<T>(lhs) += rhs
complex<T>(lhs) -= rhs
complex<T>(lhs) *= rhs
complex<T>(lhs) /= rhs
Because template argument deduction does not consider implicit conversions, these operators cannot be used for mixed integer/complex arithmetic. In all cases, the scalar must have the same type as the underlying type of the complex number.
#include <iostream> #include <complex> int main() { std::complex<double> c2(2, 0); std::complex<double> ci(0, 1); std::cout << ci << " + " << c2 << " = " << ci+c2 << '\n' << ci << " * " << ci << " = " << ci*ci << '\n' << ci << " + " << c2 << " / " << ci << " = " << ci+c2/ci << '\n' << 1 << " / " << ci << " = " << 1./ci << '\n'; // std::cout << 1.f/ci; // compile error // std::cout << 1/ci; // compile error }
Output:
(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)
compound assignment of two complex numbers or a complex and a scalar (public member function) |
|
applies unary operators to complex numbers (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/complex/operator_arith3