std::chrono::operator<<(std::chrono::year)
来自cppreference.com
| 在标头 <chrono> 定义
|
||
| |
(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>("...") 当 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 的格式化支持 (类模板特化) |