名前空間
変種
操作

std::chrono::duration<Rep,Period>::max

From cppreference.com
< cpp‎ | chrono‎ | duration
 
 
 
 
static constexpr duration max();
(C++20まで)
static constexpr duration max() noexcept;
(C++20以降)

最大の可能な値を持つ期間を返します。

期間の表現repが、最大長の期間を返すために他の実装を必要とする場合、std::chrono::duration_valuesを特殊化することで、目的の値が返されるようにできます。

目次

[編集] パラメータ

(なし)

[編集] 戻り値

duration(std::chrono::duration_values<rep>::max())

[編集]

#include <chrono>
#include <cstdint>
#include <iomanip>
#include <iostream>
 
int main()
{
    constexpr uint64_t chrono_years_max = std::chrono::years::max().count();
    constexpr uint64_t chrono_seconds_max = std::chrono::seconds::max().count();
 
    constexpr uint64_t age_of_universe_in_years{13'787'000'000}; // Λ-CDM ≈ k₁/H₀ = k₂/42
    constexpr uint64_t seconds_per_year{365'25 * 24 * 36}; // 365¼ × 24 × 60 × 60
    constexpr uint64_t age_of_universe_in_seconds{age_of_universe_in_years *
                                                  seconds_per_year};
    std::cout
        << std::scientific << std::setprecision(2)
        << "The Age of the Universe is ≈ "
        << static_cast<double>(age_of_universe_in_years) << " years or "
        << static_cast<double>(age_of_universe_in_seconds) << " seconds.\n\n"
        << "chrono::years::max() = " << chrono_years_max
        << ", sizeof(chrono::years) = "
        << sizeof(std::chrono::years) << " bytes.\n" "chrono::years "
        << (age_of_universe_in_years <= chrono_years_max ? "CAN" : "CANNOT")
        << " keep the Age of the Universe in YEARS.\n\n"
        << "chrono::seconds::max() = " << chrono_seconds_max
        << ", sizeof(chrono::seconds) = "
        << sizeof(std::chrono::seconds) << " bytes.\n" "chrono::seconds "
        << (age_of_universe_in_seconds <= chrono_seconds_max ? "CAN" : "CANNOT")
        << " keep the Age of the Universe in SECONDS.\n";
}

実行結果の例

The Age of the Universe is ≈ 1.38e+10 years or 4.35e+17 seconds.
 
chrono::years::max() = 2147483647, sizeof(chrono::years) = 4 bytes.
chrono::years CANNOT keep the Age of the Universe in YEARS.
 
chrono::seconds::max() = 9223372036854775807, sizeof(chrono::seconds) = 8 bytes.
chrono::seconds CAN keep the Age of the Universe in SECONDS.

[編集] 関連項目

[static]
特別なduration値ゼロを返す
(public staticメンバー関数) [編集]
[static]
特別なduration値minを返す
(public staticメンバー関数) [編集]
English 日本語 中文(简体) 中文(繁體)