SysTutorials

Linux Manual Pages › Section 3

stdliteralscomplex_literalsoperatorioperatorifoperatoril(3)

std::literals::complex_literals::operator""i,operator""if,operator""il - std::literals::complex_literals::operator""i,operator""if,operator""il

Synopsis

Defined in header<complex>
    constexpr complex<double> operator""i(long double arg);
(1)(since C++ 14)
    constexpr complex<double>
    operator""i(unsigned long long arg);
constexpr complex<float> operator""if(long double arg);
(2)(since C++ 14)
    constexpr complex<float>
    operator""if(unsigned long long arg);
constexpr complex<long double> operator""il(long double arg);
(3)(since C++ 14)
    constexpr complex<long double>
    operator""il(unsigned long long arg);

Forms a std::complex literal representing an imaginary number.

  1. forms a literal std::complex<double> with the real part zero and imaginary part arg
  2. forms a literal std::complex<float> with the real part zero and imaginary part arg
  3. forms a literal std::complex<long double> with the real part zero and imaginary part arg

Parameters

arg - the value of the imaginary number

Return value

The std::complex literal with the real part zero and imaginary part arg

Notes

These operators are declared in the namespace std::literals::complex_literals, where both literals and complex_literals are inline namespaces. Access to these operators can be gained with using namespace std::literals, using namespace std::complex_literals, and using namespace std::literals::complex_literals. Even though if is a keyword in C++, it is a ud-suffix of the literal_operator of the form operator ""if and in the literal expressions such as 1if or 1.0if because it is not separated by whitespace and is not a standalone token.

Possible implementation

First version

  constexpr std::complex<double> operator""i(unsigned long long d)   {       return std::complex<double>{0.0, static_cast<double>(d)};   }   constexpr std::complex<double> operator""i(long double d)   {       return std::complex<double>{0.0, static_cast<double>(d)};   }

Second version

  constexpr std::complex<float> operator""if(unsigned long long d)   {       return std::complex<float>{0.0f, static_cast<float>(d)};   }   constexpr std::complex<float> operator""if(long double d)   {       return std::complex<float>{0.0f, static_cast<float>(d)};   }

Third version

  constexpr std::complex<long double> operator""il(unsigned long long d)   {       return std::complex<long double>{0.0L, static_cast<long double>(d)};   }   constexpr std::complex<long double> operator""il(long double d)   {       return std::complex<long double>{0.0L, d};   }

Example

// Run this code #include <iostream>

#include <complex> int main()

{ using namespace std::complex_literals; std::complex<double> c = 1.0 + 1i; std::cout << "abs" << c << " = " << abs(c) << ‘\n’; }

Output:

  abs(1,1) = 1.41421

See also

constructs a complex number
constructor   (public member function)
assigns the contents
operator=     (public member function)