std::chrono::operator<<(std::chrono::year)
來自 cppreference.com
定義於標頭檔案 <chrono> |
||
template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& |
(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
,並且是 L"..." 如果 CharT
是 wchar_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 的格式化支援(類模板特化) |