名前空間
変種
操作

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

From cppreference.com
< cpp‎ | chrono‎ | year
 
 
 
 
ヘッダー <chrono> で定義
template< class CharT, class Traits >

std::basic_ostream<CharT, Traits>&

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

4桁未満の場合は、yに格納されている年を10進数としてフォーマットし、必要に応じて先頭を0で埋めて4桁にします。この文字列はsという名前で表されます。次に、!y.ok()の場合は、フォーマットされた文字列に" is not a valid year" を追加します。この文字列をosに挿入します。

以下と等価です。

return os << (y.ok() ?
    std::format(STATICALLY_WIDEN<CharT>("{:%Y}"), y) :
    std::format(STATICALLY_WIDEN<CharT>("{:%Y} is not a valid year"), y));

ここで、STATICALLY_WIDEN<CharT>("...") は、CharTchar の場合は "..."CharTwchar_t の場合は L"..." です。

[編集] 返り値

os

[編集]

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

実行結果の例

2020
-0016
-32768 is not a valid year

[編集] 関連項目

(C++20)
引数のフォーマット済み表現を新しい文字列に格納する
(関数テンプレート) [編集]
year の書式設定サポート
(クラス テンプレートの特殊化) [edit]
English 日本語 中文(简体) 中文(繁體)