命名空间
变体
操作

std::literals::chrono_literals::operator""y

来自 cppreference.cn
< cpp‎ | chrono
 
 
 
 
定义于头文件 <chrono>
constexpr std::chrono::year operator""y( unsigned long long y ) noexcept;
(C++20 起)

构造一个 std::chrono::year 字面量,表示前推格里高利历中的一年。

目录

[编辑] 参数

y - 年份值

[编辑] 返回值

一个从 int(y) 初始化的 std::chrono::year。如果 y 不在 [-3276732767] 范围内,则存储的值是未指定的。

[编辑] 可能的实现

constexpr std::chrono::year operator""y(unsigned long long y) noexcept
{
    return std::chrono::year(static_cast<int>(y));
}

[编辑] 注意

此运算符声明于命名空间 std::literals::chrono_literals 中,其中 literalschrono_literals 都是内联命名空间。可通过以下方式访问此运算符:

  • using namespace std::literals,
  • using namespace std::chrono_literals,或
  • using namespace std::literals::chrono_literals.

此外,在命名空间 std::chrono 中,标准库提供了指令 using namespace literals::chrono_literals;,因此如果程序员使用 using namespace std::chrono; 来访问 chrono 库中的类,相应的字面量运算符也会变得可见。

[编辑] 示例

#include <chrono>
#include <iostream>
 
int main()
{
    using namespace std::literals;
 
    std::cout << int(2020y)  << '\t' << 2020y  << '\n'
              << int(-220y)  << '\t' << -220y  << '\n'
              << int(3000y)  << '\t' << 3000y  << '\n'
              << int(32768y) << '\t' << 32768y << '\n'  // unspecified
              << int(65578y) << '\t' << 65578y << '\n'; // unspecified
}

可能的输出

2020	2020
-220	-0220
3000	3000
-32768	-32768 is not a valid year
42	0042

[编辑] 参阅

构造一个 year
(std::chrono::year 的公开成员函数) [编辑]