命名空间
变体
操作

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

来自 cppreference.com
< cpp‎ | chrono‎ | duration
 
 
工具库
语言支持
类型支持 (基本类型,RTTI)
库功能测试宏 (C++20)
动态内存管理
程序工具
协程支持 (C++20)
可变参数函数
调试支持
(C++26)
三方比较
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (在 C++20 中已弃用)
整数比较函数
(C++20)(C++20)(C++20)   
(C++20)
交换类型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
通用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
基本字符串转换
(C++17)
(C++17)

 
 
 
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.

[编辑] 另请参阅

[静态]
返回特殊的持续时间值零
(公共静态成员函数) [编辑]
[静态]
返回特殊的持续时间值 min
(公共静态成员函数) [编辑]