Namespaces
Variants

std::chrono:: operator<< (std::chrono::year)

From cppreference.net
Definiert in Header <chrono>
template < class CharT, class Traits >

std:: basic_ostream < CharT, Traits > &

operator << ( std:: basic_ostream < CharT, Traits > & os, const std:: chrono :: year & y ) ;
(seit C++20)

Bildet einen std:: basic_string < CharT > s , der aus dem in y gespeicherten Jahreswert besteht, formatiert als Dezimalzahl, links aufgefüllt mit 0 auf vier Stellen, falls das Ergebnis sonst weniger als vier Stellen hätte. Dann, wenn ! y. ok ( ) , wird " is not a valid year" an die formatierte Zeichenkette angehängt. Fügt diese Zeichenkette in os ein.

Entspricht

return os << ( y. ok ( ) ?
std:: format ( STATICALLY_WIDEN < CharT > ( "{:%Y}" ) , y ) :
std:: format ( STATICALLY_WIDEN < CharT > ( "{:%Y} ist kein gültiges Jahr" ) , y ) ) ;

wobei STATICALLY_WIDEN < CharT > ( "..." ) gleich "..." ist, wenn CharT gleich char ist, und L "..." wenn CharT gleich wchar_t ist.

Rückgabewert

os

Beispiel

#include <chrono>
#include <iostream>
int main()
{
    constexpr std::chrono::year y1{2020}, y2{-020}, y3{98304};
    std::cout << y1 << '\n'
              << y2 << '\n'
              << y3 << '\n';
}

Mögliche Ausgabe:

2020
-0016
-32768 is not a valid year

Siehe auch

(C++20)
speichert formatierte Darstellung der Argumente in einem neuen String
(Funktions-Template)
Formatierungsunterstützung für year
(Klassen-Template-Spezialisierung)