diff mbox

[libstdc++,C++17] Implement C++17 P0330 size_t UDL.

Message ID aca30f43-620d-a12f-a74b-73cf6be1473c@verizon.net
State New
Headers show

Commit Message

Ed Smith-Rowland July 21, 2016, 4:18 a.m. UTC
This patch defines

   operator""zu(unsigned long long __n)

for size_t literals.

for (auto k = 0zul; k < v.size(); ++k)

    ...


Testing on x86-64-linux is finishing but I'm past these tests.

OK?


Ed
2016-07-21  Edward Smith-Rowland  <3dw4rd@verizon.net>

	Implement C++17 P0330 size_t UDL.
	* include/c_global/cstddef: Add size_t operator""zu().
	* testsuite/18_support/headers/cstddef/literals/types.cc: New test.
	* testsuite/18_support/headers/cstddef/literals/values.cc: New test.

Comments

Jonathan Wakely July 21, 2016, 6:30 a.m. UTC | #1
On 21/07/16 00:18 -0400, Ed Smith-Rowland wrote:
>This patch defines
>
>  operator""zu(unsigned long long __n)
>
>for size_t literals.
>
>for (auto k = 0zul; k < v.size(); ++k)
>
>   ...
>
>
>Testing on x86-64-linux is finishing but I'm past these tests.
>
>OK?

P0330 isn't in C++17. In Oulu LEWG voted to forward it to LWG for
C++Next (i.e. C++20) and LFTSv3. I don't think LWG reviewed it yet.

https://issues.isocpp.org/show_bug.cgi?id=77
diff mbox

Patch

Index: include/c_global/cstddef
===================================================================
--- include/c_global/cstddef	(revision 238557)
+++ include/c_global/cstddef	(working copy)
@@ -57,4 +57,20 @@ 
 }
 #endif
 
+#if __cplusplus >= 201500L
+#define __cpp_lib_support_udls 201605
+namespace std
+{
+inline namespace literals
+{
+inline namespace support_literals
+{
+  constexpr size_t
+  operator""zu(unsigned long long __n)
+  { return static_cast<size_t>(__n); }
+}
+}
+}
+#endif // C++17
+
 #endif // _GLIBCXX_CSTDDEF
Index: testsuite/18_support/headers/cstddef/literals/types.cc
===================================================================
--- testsuite/18_support/headers/cstddef/literals/types.cc	(nonexistent)
+++ testsuite/18_support/headers/cstddef/literals/types.cc	(working copy)
@@ -0,0 +1,31 @@ 
+// { dg-options "-std=gnu++17" }
+// { dg-do compile }
+
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <cstddef>
+#include <type_traits>
+
+void
+test01()
+{
+  using namespace std::literals::support_literals;
+
+  static_assert(std::is_same<decltype(1zu), std::size_t>::value,
+		"1zu is std::size_t");
+}
Index: testsuite/18_support/headers/cstddef/literals/values.cc
===================================================================
--- testsuite/18_support/headers/cstddef/literals/values.cc	(nonexistent)
+++ testsuite/18_support/headers/cstddef/literals/values.cc	(working copy)
@@ -0,0 +1,42 @@ 
+// { dg-options "-std=gnu++1z" }
+// { dg-do run }
+
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <cstddef>
+#include <limits>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  using namespace std::literals::support_literals;
+  bool test [[gnu::unused]] = true;
+
+  std::size_t s = 1zu;
+  std::size_t t = -1zu;
+
+  VERIFY( s == 1 );
+  VERIFY( t == std::numeric_limits<std::size_t>::max() );
+}
+
+int
+main()
+{
+  test01();
+}