From f182038521c64fa2dfb4c02a620f687c0c37cca0 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 24 Jul 2013 21:18:14 +0000 Subject: [PATCH] literal suffixes for std::chrono git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@187078 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/chrono | 91 +++++++++++++++++++ .../time/time.duration/time.duration.literals | 59 ++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/utilities/time/time.duration/time.duration.literals diff --git a/include/chrono b/include/chrono index c6eef8fd..219e20e7 100644 --- a/include/chrono +++ b/include/chrono @@ -259,6 +259,19 @@ typedef steady_clock high_resolution_clock; } // chrono +constexpr chrono::hours operator "" h(unsigned long long); // C++14 +constexpr chrono::duration> operator "" h(long double); // C++14 +constexpr chrono::minutes operator "" min(unsigned long long); // C++14 +constexpr chrono::duration> operator "" min(long double); // C++14 +constexpr chrono::seconds operator "" s(unsigned long long); // C++14 +constexpr chrono::duration operator "" s(long double); // C++14 +constexpr chrono::milliseconds operator "" ms(unsigned long long); // C++14 +constexpr chrono::duration operator "" ms(long double); // C++14 +constexpr chrono::microseconds operator "" us(unsigned long long); // C++14 +constexpr chrono::duration operator "" us(long double); // C++14 +constexpr chrono::nanoseconds operator "" ns(unsigned long long); // C++14 +constexpr chrono::duration operator "" ns(long double); // C++14 + } // std */ @@ -893,6 +906,84 @@ typedef steady_clock high_resolution_clock; } // chrono +#if _LIBCPP_STD_VER > 11 +// Literal suffixes for chrono types +// inline // Deviation from N3690. +// We believe the inline to be a defect and have submitted an LWG issue. +// An LWG issue number has not yet been assigned. +namespace literals +{ + inline namespace chrono_literals + { + + constexpr chrono::hours operator"" h(unsigned long long __h) + { + return chrono::hours(static_cast(__h)); + } + + constexpr chrono::duration> operator"" h(long double __h) + { + return chrono::duration>(__h); + } + + + constexpr chrono::minutes operator"" min(unsigned long long __m) + { + return chrono::minutes(static_cast(__m)); + } + + constexpr chrono::duration> operator"" min(long double __m) + { + return chrono::duration> (__m); + } + + + constexpr chrono::seconds operator"" s(unsigned long long __s) + { + return chrono::seconds(static_cast(__s)); + } + + constexpr chrono::duration operator"" s(long double __s) + { + return chrono::duration (__s); + } + + + constexpr chrono::milliseconds operator"" ms(unsigned long long __ms) + { + return chrono::milliseconds(static_cast(__ms)); + } + + constexpr chrono::duration operator"" ms(long double __ms) + { + return chrono::duration(__ms); + } + + + constexpr chrono::microseconds operator"" us(unsigned long long __us) + { + return chrono::microseconds(static_cast(__us)); + } + + constexpr chrono::duration operator"" us(long double __us) + { + return chrono::duration (__us); + } + + + constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns) + { + return chrono::nanoseconds(static_cast(__ns)); + } + + constexpr chrono::duration operator"" ns(long double __ns) + { + return chrono::duration (__ns); + } + +}} +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_CHRONO diff --git a/test/utilities/time/time.duration/time.duration.literals b/test/utilities/time/time.duration/time.duration.literals new file mode 100644 index 00000000..366f628a --- /dev/null +++ b/test/utilities/time/time.duration/time.duration.literals @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +#include +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + using namespace std::literals::chrono_literals; + +// Make sure the types are right + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + + std::chrono::hours h = 4h; + assert ( h == std::chrono::hours(4)); + auto h2 = 4.0h; + assert ( h == h2 ); + + std::chrono::minutes min = 36min; + assert ( min == std::chrono::minutes(36)); + auto min2 = 36.0min; + assert ( min == min2 ); + + std::chrono::seconds s = 24s; + assert ( s == std::chrono::seconds(24)); + auto s2 = 24.0s; + assert ( s == s2 ); + + std::chrono::milliseconds ms = 247ms; + assert ( ms == std::chrono::milliseconds(247)); + auto ms2 = 247.0ms; + assert ( ms == ms2 ); + + std::chrono::microseconds us = 867us; + assert ( us == std::chrono::microseconds(867)); + auto us2 = 867.0us; + assert ( us == us2 ); + + std::chrono::nanoseconds ns = 645ns; + assert ( ns == std::chrono::nanoseconds(645)); + auto ns2 = 645.ns; + assert ( ns == ns2 ); +#endif +}