次の方法で共有


month クラス

年の月を表します。 たとえば、July です。

構文

class month; // C++20

解説

month は、通常、[1, 12] の範囲の値を保持します。 また、この範囲外の負でない値を保持することもできます。 month クラスで使用できる定数については、次の「Month 定数」を参照してください。

メンバー

名前 説明
コンストラクター month を構築します。
ok 月の値が有効な範囲 [1,12] にあることを確認します。
operator++ month をインクリメントします。
operator+= 指定された月数をこの month に追加します。
operator-- この month をデクリメントします。
operator-= この month から指定された月数を減算します。
operator unsigned month 値を取得します。

非メンバー

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

必要条件

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

名前空間std::chrono:

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

コンストラクター

month を構築します。

1) month() = default;
2) explicit constexpr month(unsigned m) noexcept;

パラメーター

m
month を値 m を使用して構築します。

解説

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

ok

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

constexpr bool ok() const noexcept;

戻り値

月の値が [1, 12] の範囲内にある場合は true。 それ以外の場合は false

operator++

月の値を増分します。

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

戻り値

1) インクリメント後*this の月の参照 (後置インクリメント)。
2) インクリメント前month のコピー (前置インクリメント)。

例: operator++

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

using namespace std::chrono;

int main()
{
    month m{ January };
    month m2{4}; // April

    std::cout << m << " " << ++m << "\n"; // constexpr month& operator++() noexcept
    std::cout << m << " " << m++ << "\n"; // constexpr month operator++(int) noexcept
    std::cout << m << "\n";
    std::cout << m2 << "\n";

    return 0;
}
Jan Feb
Feb Feb
Mar
Apr

解説

結果が 12 を超える場合は、1 に設定されます。

operator--

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

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

戻り値

1) デクリメント*thismonth への参照 (後置デクリメント)。
2) デクリメントmonth のコピー (前置デクリメント)。

例: operator--

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

using namespace std;
using namespace std::chrono;

int main()
{
    month m{May};

    cout << m << " " << --m << "\n"; // constexpr month& operator++() noexcept
    cout << m << " " << m-- << "\n"; // constexpr month operator++(int) noexcept
    cout << m << "\n";

    return 0;
}
May Apr
Apr Apr
Mar

解説

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

operator+=

この month に月を追加します。

constexpr month& operator+=(const months& m) noexcept;

パラメーター

m
加算する月数。

戻り値

*this

operator-=

months をこの month から減算します。

constexpr month& operator-=(const months& m) noexcept;

パラメーター

m
減算する月。

戻り値

*this.

operator unsigned

符号なしの month の値を取得します。

explicit constexpr operator unsigned() const noexcept;

戻り値

この month の符号なしの値

例: operator unsigned()

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

using namespace std::chrono;

int main()
{
    month m{July};
    unsigned monthValue = static_cast<unsigned>(m);
    std::cout << monthValue << "\n";

    return 0;
}
7

Month 定数

(C++20) <chrono> ヘッダーでは、month と使用できる次の定数を定義します。これを使用することで、コードの利便性、タイプ セーフ、保守性が向上します。 これらの定数は、std::chrono がスコープ内にある場合にスコープ内に入っています。

// Calendrical constants
inline constexpr month January{1};
inline constexpr month February{2};
inline constexpr month March{3};
inline constexpr month April{4};
inline constexpr month May{5};
inline constexpr month June{6};
inline constexpr month July{7};
inline constexpr month August{8};
inline constexpr month September{9};
inline constexpr month October{10};
inline constexpr month November{11};
inline constexpr month December{12};

関連項目

<chrono>
month_day クラス
month_day_last クラス
month_weekday クラス
month_weekday_last クラス