名稱空間
變體
操作

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

來自 cppreference.com
< 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 的公開成員函式) [編輯]