chrono
文字
(C++14)標頭 <chrono>
會定義12 個使用者定義的常值,這些常值 代表小時、分鐘、秒、毫秒、微秒和奈秒。 每個使用者定義常值皆有整數和浮點多載。 常值會定義於內嵌命名空間中 literals::chrono_literals
,這會在範圍中時自動 std::chrono
帶入範圍。
語法
inline namespace literals {
inline namespace chrono_literals {
// return integral days
constexpr std::chrono::day operator"" d(unsigned long long d) noexcept;
// return integral hours
constexpr chrono::hours operator"" h(unsigned long long Val);
// return floating-point hours
constexpr chrono::duration<double, ratio<3600>> operator"" h(long double Val);
// return integral minutes
constexpr chrono::minutes(operator"" min)(unsigned long long Val);
// return floating-point minutes
constexpr chrono::duration<double, ratio<60>>(operator"" min)(long double Val);
// return integral seconds
constexpr chrono::seconds operator"" s(unsigned long long Val);
// return floating-point seconds
constexpr chrono::duration<double> operator"" s(long double Val);
// return integral milliseconds
constexpr chrono::milliseconds operator"" ms(unsigned long long Val);
// return floating-point milliseconds
constexpr chrono::duration<double, milli> operator"" ms(long double Val);
// return integral microseconds
constexpr chrono::microseconds operator"" us(unsigned long long Val);
// return floating-point microseconds
inline constexpr chrono::duration<double, micro> operator"" us(long double Val);
// return integral nanoseconds
inline constexpr chrono::nanoseconds operator"" ns(unsigned long long Val);
// return floating-point nanoseconds
constexpr chrono::duration<double, nano> operator"" ns(long double Val);
// return integral year
constexpr chrono::year operator""y(unsigned long long y) noexcept; // C++20
} // inline namespace chrono_literals
} // inline namespace literals
傳回值
接受 long long
引數的常值會傳回值或對應類型。 接受浮點自變數的常值會傳回 duration
。
範例
下列範例示範如何使用 chrono
常值。
constexpr auto day = 14d; // If the value > 255, then the stored value is unspecified.
constexpr auto twoDays = 48h;
constexpr auto week = 24h* 7;
constexpr auto my_duration_unit = 108ms;
constexpr auto theYear = 2021y;