Section
std::complex
Specializations of std::complex for cv-unqualified standard(until C++23) floating-point types are TriviallyCopyable(since C++23) LiteralTypes for representing and manipulating complex number.
# Declarations
template< class T >
class complex;
template<> class complex<float>;
(until C++23)
template<> class complex<double>;
(until C++23)
template<> class complex<long double>;
(until C++23)
# Notes
Feature-test macro
Value
Std
Feature
__cpp_lib_constexpr_complex
201711L
(C++20)
constexpr simple complex mathematical functions in
# Example
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <ranges>
int main()
{
using namespace std::complex_literals;
std::cout << std::fixed << std::setprecision(1);
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
std::cout << "i * i = " << z1 << '\n';
std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
std::cout << "pow(i, 2) = " << z2 << '\n';
const double PI = std::acos(-1); // or std::numbers::pi in C++20
std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
std::cout << "exp(i * pi) = " << z3 << '\n';
std::complex<double> z4 = 1.0 + 2i, z5 = 1.0 - 2i; // conjugates
std::cout << "(1 + 2i) * (1 - 2i) = " << z4 * z5 << '\n';
const auto zz = {0.0 + 1i, 2.0 + 3i, 4.0 + 5i};
#if __cpp_lib_tuple_like >= 202311L
for (double re : zz | std::views::keys)
std::cout << re << ' ';
std::cout << '\n';
for (double im : zz | std::views::values)
std::cout << im << ' ';
std::cout << '\n';
#else
for (double re : zz | std::views::transform([](auto z){ return z.real(); }))
std::cout << re << ' ';
std::cout << '\n';
for (double im : zz | std::views::transform([](auto z){ return z.imag(); }))
std::cout << im << ' ';
std::cout << '\n';
#endif
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 387 | C++98 | std::complex was not guaranteed to be compatible with C complex | guaranteed to be compatible |