chrono
littéraux
(C++14) L’en-tête <chrono>
définit 12 littéraux définis par l’utilisateur qui représentent des heures, des minutes, des secondes, des millisecondes, des microsecondes et des nanosecondes. Chaque littéral défini par l'utilisateur a une intégrale et une surcharge à virgule flottante. Les littéraux sont définis dans l’espace de noms inline, qui est placé automatiquement dans l’étendue literals::chrono_literals
lorsqu’il std::chrono
est dans l’étendue.
Syntaxe
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
Valeur retournée
Les littéraux qui acceptent un argument long long
retournent une valeur ou le type correspondant. Les littéraux qui prennent un argument à virgule flottante retournent un duration
.
Exemple
Les exemples suivants montrent comment utiliser les chrono
littéraux.
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;
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour