Namensräume
Varianten
Aktionen

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

Von cppreference.com
< cpp‎ | numeric‎ | complex
 
 
 
 
Definiert in der Header-Datei <complex>
constexpr complex<double> operator""i( long double arg );
constexpr complex<double> operator""i( unsigned long long arg );
(1) (seit C++14)
constexpr complex<float> operator""if( long double arg );
constexpr complex<float> operator""if( unsigned long long arg );
(2) (seit C++14)
constexpr complex<long double> operator""il( long double arg );
constexpr complex<long double> operator""il( unsigned long long arg );
(3) (seit C++14)

Bildet ein std::complex-Literal, das eine imaginäre Zahl darstellt.

1) Bildet ein Literal std::complex<double> mit dem Realteil Null und dem Imaginärteil arg.
2) Bildet ein Literal std::complex<float> mit dem Realteil Null und dem Imaginärteil arg.
3) Bildet ein Literal std::complex<long double> mit dem Realteil Null und dem Imaginärteil arg.

Inhalt

[edit] Parameter

arg - der Wert der imaginären Zahl

[edit] Rückgabewert

Das std::complex-Literal mit dem Realteil Null und dem Imaginärteil arg.

[edit] Anmerkungen

Diese Operatoren sind im Namespace std::literals::complex_literals deklariert, wobei sowohl literals als auch complex_literals Inline-Namespaces sind. Der Zugriff auf diese Operatoren kann entweder mit

  • using namespace std::literals,
  • using namespace std::complex_literals oder
  • using namespace std::literals::complex_literals.

erfolgen. Obwohl if ein Schlüsselwort in C++ ist, handelt es sich um ein ud-suffix des Literaloperators der Form operator ""if und in den Literal-Ausdrücken wie 1if oder 1.0if, da es nicht durch Leerzeichen getrennt ist und kein eigenständiger Token ist.

Feature-Test-Makro Wert Std Feature
__cpp_lib_complex_udls 201309L (C++14) User-Defined Literals für std::complex

[edit] Mögliche Implementierung

operator""i
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)};
}
operator""if
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)};
}
operator""il
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};
}

[edit] Beispiel

#include <complex>
#include <iostream>
 
int main()
{
    using namespace std::complex_literals;
 
    std::complex<double> c = 1.0 + 1i;
    std::cout << "abs" << c << " = " << std::abs(c) << '\n';
 
    std::complex<float> z = 3.0f + 4.0if;
    std::cout << "abs" << z << " = " << std::abs(z) << '\n';
}

Ausgabe

abs(1,1) = 1.41421
abs(3,4) = 5

[edit] Siehe auch

konstruiert eine komplexe Zahl
(public member function) [edit]
weist den Inhalt zu
(public member function) [edit]