命名空间
变体
操作

std::chrono::operator<<(std::chrono::day)

来自 cppreference.cn
< cpp‎ | chrono‎ | day
 
 
 
 
定义于头文件 <chrono>
template< class CharT, class Traits >

std::basic_ostream<CharT, Traits>&

    operator<<( std::basic_ostream<CharT, Traits>& os, const std::chrono::day& d );
(自 C++20 起)

构成一个 std::basic_string<CharT> s,它由存储在 d 中的 day 值格式化为十进制数组成,如果结果原本是单个十进制数字,则带有前导零。然后,如果 !d.ok(),则将 " is not a valid day" 附加到格式化后的字符串。将该字符串插入到 os 中。

等价于

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

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

[编辑] 返回值

os

[编辑] 示例

#include <chrono>
#include <iostream>
 
int main()
{
    constexpr std::chrono::day d1{31}, d2{7}, d3{42}, d4{};
    std::cout << d1 << '\n'
              << d2 << '\n'
              << d3 << '\n'
              << d4 << '\n';
}

可能的输出

31
07
42 is not a valid day
00 is not a valid day

[编辑] 参见

(C++20)
将参数的格式化表示形式存储在新字符串中
(函数模板) [编辑]
day 的格式化支持
(类模板特化) [编辑]