Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/complex/operator""i"

From cppreference.com
< cpp‎ | numeric‎ | complex
(note that if isn't a keyword here)
m (correct if note a bit)
Line 47: Line 47:
 
These operators are declared in the namespace {{c|std::literals::complex_literals}}, where both {{tt|literals}} and {{tt|complex_literals}} are inline namespaces. Access to these operators can be gained with {{c|using namespace std::literals}} and {{c|using namespace std::literals::complex_literals}}.
 
These operators are declared in the namespace {{c|std::literals::complex_literals}}, where both {{tt|literals}} and {{tt|complex_literals}} are inline namespaces. Access to these operators can be gained with {{c|using namespace std::literals}} and {{c|using namespace std::literals::complex_literals}}.
  
Even though {{c|if}} is a [[cpp/keywords/if|keyword]] in C++, it is treated as the {{spar|ud-suffix}} of the [[cpp/language/user_literal|user-defined literal]] in the string {{c|operator""if}} and in the literal expressions such as {{c|1if}} or {{c|1.0if}}.
+
Even though {{c|if}} is a [[cpp/keywords/if|keyword]] in C++, it is a {{spar|ud-suffix}} of the [[cpp/language/user_literal|literal operator]] of the form {{c|operator ""if}} and in the literal expressions such as {{c|1if}} or {{c|1.0if}} because it is not separated by whitespace and is not a standalone token.
  
 
===Example===
 
===Example===

Revision as of 10:48, 6 June 2014

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

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

Contents

Parameters

arg - the value of the imaginary number

Return value

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

Possible implementation

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

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 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.

Example

#include <iostream>
#include <complex>
 
int main()
{
    using namespace std::literals::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
(public member function)