命名空间
变体
操作

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

来自 cppreference.cn
< 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 起)

构成 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} is not a valid year"), y));

其中 STATICALLY_WIDEN<CharT>("...")"..." ,如果 CharTchar,而 L"..." ,如果 CharTwchar_t

[编辑] 返回值

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 的格式化支持
(类模板特化) [编辑]