[libcxx] r187078 - literal suffixes for std::chrono

Marshall Clow mclow.lists at gmail.com
Wed Jul 24 14:18:14 PDT 2013


Author: marshall
Date: Wed Jul 24 16:18:14 2013
New Revision: 187078

URL: http://llvm.org/viewvc/llvm-project?rev=187078&view=rev
Log:
literal suffixes for std::chrono

Added:
    libcxx/trunk/test/utilities/time/time.duration/time.duration.literals
Modified:
    libcxx/trunk/include/chrono

Modified: libcxx/trunk/include/chrono
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/chrono?rev=187078&r1=187077&r2=187078&view=diff
==============================================================================
--- libcxx/trunk/include/chrono (original)
+++ libcxx/trunk/include/chrono Wed Jul 24 16:18:14 2013
@@ -259,6 +259,19 @@ typedef steady_clock high_resolution_clo
 
 }  // chrono
 
+constexpr chrono::hours                                 operator "" h(unsigned long long); // C++14
+constexpr chrono::duration<unspecified , ratio<3600,1>> operator "" h(long double); // C++14
+constexpr chrono::minutes                               operator "" min(unsigned long long); // C++14
+constexpr chrono::duration<unspecified , ratio<60,1>>   operator "" min(long double); // C++14
+constexpr chrono::seconds                               operator "" s(unsigned long long); // C++14
+constexpr chrono::duration<unspecified >                operator "" s(long double); // C++14
+constexpr chrono::milliseconds                          operator "" ms(unsigned long long); // C++14
+constexpr chrono::duration<unspecified , milli>         operator "" ms(long double); // C++14
+constexpr chrono::microseconds                          operator "" us(unsigned long long); // C++14
+constexpr chrono::duration<unspecified , micro>         operator "" us(long double); // C++14
+constexpr chrono::nanoseconds                           operator "" ns(unsigned long long); // C++14
+constexpr chrono::duration<unspecified , nano>          operator "" ns(long double); // C++14
+
 }  // std
 */
 
@@ -893,6 +906,84 @@ typedef steady_clock high_resolution_clo
 
 } // 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<chrono::hours::rep>(__h));
+	}
+
+	constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h)
+	{
+		return chrono::duration<long double, ratio<3600,1>>(__h);
+	}
+
+
+	constexpr chrono::minutes operator"" min(unsigned long long __m)
+	{
+		return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
+	}
+
+	constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m)
+	{
+		return chrono::duration<long double, ratio<60,1>> (__m);
+	}
+
+
+	constexpr chrono::seconds operator"" s(unsigned long long __s)
+	{
+		return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
+	}
+
+	constexpr chrono::duration<long double> operator"" s(long double __s)
+	{
+		return chrono::duration<long double> (__s);
+	}
+
+
+	constexpr chrono::milliseconds operator"" ms(unsigned long long __ms)
+	{
+		return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
+	}
+
+	constexpr chrono::duration<long double, milli> operator"" ms(long double __ms)
+	{
+		return chrono::duration<long double, milli>(__ms);
+	}
+
+
+	constexpr chrono::microseconds operator"" us(unsigned long long __us)
+	{
+		return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
+	}
+
+	constexpr chrono::duration<long double, micro> operator"" us(long double __us)
+	{
+		return chrono::duration<long double, micro> (__us);
+	}
+	
+
+	constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns)
+	{
+		return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
+	}
+
+	constexpr chrono::duration<long double, nano> operator"" ns(long double __ns)
+	{
+		return chrono::duration<long double, nano> (__ns);
+	}
+
+}}
+#endif
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_CHRONO

Added: libcxx/trunk/test/utilities/time/time.duration/time.duration.literals
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/time/time.duration/time.duration.literals?rev=187078&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/time/time.duration/time.duration.literals (added)
+++ libcxx/trunk/test/utilities/time/time.duration/time.duration.literals Wed Jul 24 16:18:14 2013
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <chrono>
+
+#include <chrono>
+#include <type_traits>
+#include <cassert>
+
+int main()
+{
+#if _LIBCPP_STD_VER > 11 
+	using namespace std::literals::chrono_literals;
+
+//	Make sure the types are right
+	static_assert ( std::is_same<decltype( 3h   ), std::chrono::hours>::value, "" );
+	static_assert ( std::is_same<decltype( 3min ), std::chrono::minutes>::value, "" );
+	static_assert ( std::is_same<decltype( 3s   ), std::chrono::seconds>::value, "" );
+	static_assert ( std::is_same<decltype( 3ms  ), std::chrono::milliseconds>::value, "" );
+	static_assert ( std::is_same<decltype( 3us  ), std::chrono::microseconds>::value, "" );
+	static_assert ( std::is_same<decltype( 3ns  ), std::chrono::nanoseconds>::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
+}





More information about the cfe-commits mailing list