Namespaces
Variants

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

From cppreference.net
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)
        << "宇宙年龄约为 "
        << static_cast<double>(age_of_universe_in_years) << " 年或 "
        << static_cast<double>(age_of_universe_in_seconds) << " 秒。\n\n"
        << "chrono::years::max() = " << chrono_years_max
        << ", sizeof(chrono::years) = "
        << sizeof(std::chrono::years) << " 字节。\n" "chrono::years "
        << (age_of_universe_in_years <= chrono_years_max ? "能够" : "无法")
        << " 以年为单位存储宇宙年龄。\n\n"
        << "chrono::seconds::max() = " << chrono_seconds_max
        << ", sizeof(chrono::seconds) = "
        << sizeof(std::chrono::seconds) << " 字节。\n" "chrono::seconds "
        << (age_of_universe_in_seconds <= chrono_seconds_max ? "能够" : "无法")
        << " 以秒为单位存储宇宙年龄。\n";
}

可能的输出:

宇宙年龄约为 1.38e+10 年或 4.35e+17 秒。
chrono::years::max() = 2147483647, sizeof(chrono::years) = 4 字节。
chrono::years 无法 以年为单位存储宇宙年龄。
chrono::seconds::max() = 9223372036854775807, sizeof(chrono::seconds) = 8 字节。
chrono::seconds 能够 以秒为单位存储宇宙年龄。

参见

[static]
返回特殊的时长值 zero
(公开静态成员函数)
[static]
返回特殊的时长值 min
(公开静态成员函数)