std::chrono::duration<Rep,Period>::max
来自cppreference.com
| (C++20 前) | ||
| (C++20 起) | ||
返回拥有最大可能值的时长。
若时长表示 rep 要求某个其他实现返回最大长度的时长,则可特化 std::chrono::duration_values 以返回所欲值。
参数
(无)
返回值
duration(std::chrono::duration_values<rep>::max())
示例
Run this code
#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) << " bytes.\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 bytes.
chrono::seconds *能*以<秒>保留宇宙年龄。
参阅
[静态] |
返回特殊时长值零 (公开静态成员函数) |
[静态] |
返回特殊时长值最小值 (公开静态成员函数) |