Namespaces
Variants

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

From cppreference.net
定义于头文件 <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 起)

构造一个 std:: basic_string < CharT > s ,其中包含存储在 y 中的年份值,格式化为十进制数字。如果结果位数少于四位,则在左侧填充 0 至四位数。随后,若 ! 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} 不是有效年份" ) , y ) ) ;

其中 STATICALLY_WIDEN < CharT > ( "..." ) CharT char 时是 "..." ,当 CharT wchar_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 类型的格式化支持
(类模板特化)