次の方法で共有


year クラス

グレゴリオ暦での年を表します。

構文

class year; // C++20

解説

year は、-32767 から 32767 の間の年の値を保持できます。

メンバー

名前 説明
コンストラクター year を構築します
is_leap 年が閏年かどうかを調べます。
max 年の最大値を返します。
min 年の最小値を返します。
ok 年の値が有効な範囲 [-32767, 32767] 内にあることを確認します。
operator+ 単項プラス
operator++ 年をインクリメントします。
operator+= 指定された年数をこの year に追加します。
operator- 単項マイナス。
operator-- 年をデクリメントします。
operator-= 指定された年数をこの year から減算します。
operator int year 値を取得します。

非メンバー

名前 説明
from_stream 指定した形式を使用して、ストリームから year を解析します。
operator+ 年数を追加します。
operator- 年数を減算します。
operator== 2 つの年が等しいかどうかを判断します。
operator<=> この year を別の year と比較します。 演算子 >, >=, <=, <, != はコンパイラによって合成されます。
operator<< 指定したストリームに year を出力します。
operator""y リテラル year を作成します。

必要条件

ヘッダー:<chrono> (C++20 以降)

名前空間std::chrono:

コンパイラ オプション:/std:c++latest

コンストラクター

year を構築します。

1) year() = default;
2) explicit constexpr year(unsigned y) noexcept;

パラメーター

y
year を値 y を使用して構築します。

解説

1) 既定のコンストラクターは、year 値を初期化しません。
2) 指定された値を使用して year を構築します。

例: year を作成する

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year y{2020};
    year y2 = 2021y;
    
    std::cout << y << ", " << y2;

    return 0;
}
2020, 2021

is_leap

この year に格納された値の範囲が有効であるかを確認します。

constexpr bool is_leap() const noexcept;

戻り値

年の値が閏年である場合は true です。 それ以外の場合は false。 閏年は、4 で割り切れるが 100 では割り切れない年、または 400 で割り切れる年です。

max

年の最大値を返します。

static constexpr year max() noexcept;

戻り値

year{32767}

min

年の最小値を返します。

static constexpr year min() noexcept;

戻り値

year{-32767}

ok

この year に格納された値の範囲が有効であるかを確認します。

constexpr bool ok() const noexcept;

戻り値

年の値が [-32676, 32767] の範囲内にある場合は true です。 それ以外の場合は false

operator+

単項プラスを適用します。

constexpr year operator+() const noexcept;

戻り値

*this を返します。

例: 単項 operator+

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
   year y{-1};
   std::cout << +y;
   return 0;
}
-0001

operator++

年の値に 1 を加算します。

1) constexpr year& operator++() noexcept;
2) constexpr year operator++(int) noexcept;

戻り値

1) インクリメントのこの年への参照 (後置インクリメント) を返します。
2) インクリメントyear のコピー (前置インクリメント) を返します。

例: operator++

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year y{2021};

    std::cout << y << " " << ++y << "\n"; // constexpr year& operator++() noexcept
    std::cout << y << " " << y++ << "\n"; // constexpr year operator++(int) noexcept
    std::cout << y << "\n";
    return 0;
}
2021 2022
2022 2022
2023

解説

インクリメントされた結果が 32767 を超えた場合、-32768 にオーバーフローします

operator-

単項マイナス。 year を負数化します。

constexpr year operator-() const noexcept; // C++20

戻り値

year の負のコピーを返します。

例: 単項 operator-

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
   year y{1977};
   std::cout << -y << '\n';

   return 0;
}
-1977

operator--

年の値から 1 を減算します。

1) constexpr year& operator--() noexcept;
2) constexpr year operator--(int) noexcept;

戻り値

1) デクリメントの、この year への参照 (後置デクリメント)。
2) デクリメントyear のコピー (前置デクリメント)。

例: operator--

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
   year y{2021};

    std::cout << y << " " << --y << "\n"; // constexpr year& operator++() noexcept
    std::cout << y << " " << y-- << "\n"; // constexpr year operator++(int) noexcept
    std::cout << y << "\n";

    return 0;
}
2021 2020
2020 2020
2019

解説

デクリメントされた結果が -32768 未満の場合、32767 に設定されます。

operator+=

この year に日数を加算します。

constexpr year& operator+=(const years& y) noexcept;

パラメーター

y
加算する年数。

戻り値

*this インクリメントされた結果が 32767 を超えた場合、-32768 にオーバーフローします。

operator-=

この year から日を減算します。

constexpr year& operator-=(const years& y) noexcept;

パラメーター

y
減算する年数。

戻り値

*this= デクリメントされた結果が -32768 未満の場合、32767 に設定されます。

operator int

year 値を取得します。

explicit constexpr operator int() const noexcept;

戻り値

year タグの値

例: operator int()

// compile using: /std:c++latest

 #include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year y{2020};
    int yearValue = static_cast<int>(y);
    std::cout << yearValue;

    return 0;
}
2020

関連項目

<chrono>
year_month
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
ヘッダー ファイル リファレンス