std::complex::complex(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::complex::complex(3)

ok so first things first: this page lives in the C++ Standard Libary manual (yes, that's genuinely how it's spelled up top β€” she's iconic, leave her alone 😭) and it's all about std::complex::complex, the constructor. buckle up bestie, there are like four million overloads and we're meeting every single one πŸŽ€


✨ NAME ✨

std::complex::complex β€” std::complex::complex.

that's it, that's the name. she's a constructor, her entire brand is existing. periodt πŸ’…


🌸 SYNOPSIS 🌸

ngl the original synopsis arrived as one big column-smooshed soup (the columns got interleaved in transit 😭), so here it is all un-tangled β€” but every single declaration, every version tag and every (1)/(2)/(3) marker is still right here. no cap, nothing dropped 🫢

πŸ’œ Primary template (std::complex<T>)

complex( const T& re = T(), const T& im = T() );                 (until C++14)
constexpr complex( const T& re = T(), const T& im = T() );       (since C++14)

complex( const complex& other );                                 (until C++14)
constexpr complex( const complex& other );                       (since C++14)
constexpr complex( const complex& other ) = default;             (since C++23)

template< class X > complex( const complex<X>& other );          (until C++14)
template< class X > constexpr complex( const complex<X>& other ); (since C++14)
                                                                 (until C++23)
template< class X > constexpr explicit(/* see below */)
                    complex( const complex<X>& other );          (since C++23)

πŸ¦‹ Standard explicit specialization std::complex<float>

complex( float re = 0.0f, float im = 0.0f );                     (until C++11)
constexpr complex( float re = 0.0f, float im = 0.0f );           (since C++11)

constexpr complex( const complex<float>& other ) = default;      (since C++20)

explicit complex( const complex<double>& other );                (until C++11)
explicit complex( const complex<long double>& other );           (until C++11)
constexpr explicit complex( const complex<double>& other );      (since C++11)
constexpr explicit complex( const complex<long double>& other ); (since C++11)

🌈 Standard explicit specialization std::complex<double>

complex( double re = 0.0, double im = 0.0 );                     (until C++11)
constexpr complex( double re = 0.0, double im = 0.0 );           (since C++11)

constexpr complex( const complex<double>& other ) = default;     (since C++20)

complex( const complex<float>& other );                          (until C++11)
explicit complex( const complex<long double>& other );           (until C++11)
constexpr complex( const complex<float>& other );                (since C++11)
constexpr explicit complex( const complex<long double>& other ); (since C++11)

⭐ Standard explicit specialization std::complex<long double>

complex( long double re = 0.0L, long double im = 0.0L );         (until C++11)
constexpr complex( long double re = 0.0L,
                   long double im = 0.0L );                      (since C++11)

constexpr complex( const complex<long double>& other ) = default; (since C++20)

complex( const complex<float>& other );                          (until C++11)
complex( const complex<double>& other );                         (until C++11)
constexpr complex( const complex<float>& other );                (since C++11)
constexpr complex( const complex<double>& other );               (since C++11)

and the (1) (2) (3) markers you'll see floating around below mean:

  • (1) β€” the "make a complex out of a real part and an imaginary part" one πŸ“
  • (2) β€” the copy constructor πŸŽ€
  • (3) β€” the converting constructor πŸ‘‘

πŸ’– DESCRIPTION πŸ’–

Constructs the std::complex object. that's the whole job bestie, and she does it flawlessly ✨

The standard explicit specializations (std::complex<float>, std::complex<double> and std::complex<long double>) have different constructor declarations from the main template.

(until C++23)

1) Constructs the complex number from real part re and imaginary part im. 🧚

2) Copy constructor. Constructs the object with the copy of the contents of other.

The copy constructors are implicitly declared in the standard explicit specializations.

(until C++20)

3) Converting constructor. Constructs the object from a complex number of a different type.

The main template provides a converting constructor template, while each standard explicit specialization provides two non-template constructors for the two other standard explicit specializations.

(until C++23)

The non-template constructors are converting constructors (i.e. non-explicit) if and only if the conversions of the real and imaginary parts are not narrowing. (read that twice, it's the whole vibe check πŸ”₯)

For the main template, the expression inside explicit evaluates to false if and only if the floating-point conversion rank of T is greater than or equal to the floating-point conversion rank of X.

(since C++23)

πŸŽ€ PARAMETERS πŸŽ€

  • re β€” the real part
  • im β€” the imaginary part
  • other β€” another complex number to use as source

πŸ”₯ NOTES πŸ”₯

Since C++23, the copy constructor is required to be trivial in order to satisfy the TriviallyCopyable requirement β€” but implementations generally make it trivial in all modes. (so like, the standard finally caught up to what everyone was already doing, we love a glow-up πŸ‘‘)


🫢 SEE ALSO 🫢

  • operator= β€” assigns the contents (public member function)
  • operator""if
  • operator""i β€” a std::complex literal representing purely imaginary number
  • operator""il β€” (function) (C++14)
  • C documentation for CMPLX

ok that's the whole page, you ate that πŸ’… stay hydrated, go construct some complex numbers responsibly, and remember: narrowing conversions are not the move unless the standard says so 🌸

sign-off corner: this was brought to you by http://cppreference.com, dated 2024.06.10, and filed under std::complex::complex(3) β€” mwah, love u, bye πŸ¦‹πŸ’–
πŸ“¦ where this page comes from