public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Define std::uniform_random_bit_generator concept for C++20
@ 2019-10-24  9:35 Jonathan Wakely
  2019-10-24  9:47 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2019-10-24  9:35 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 432 bytes --]

	* include/bits/random.h (uniform_random_bit_generator): Define for
	C++20.
	* testsuite/26_numerics/random/concept.cc: New test.
	* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error line.

This is the last piece of P0898R3, "C++20 concepts library", although
other proposals added more concepty things to <iterator> and other
headers (patch incoming for that).

Tested powerpc64le-linux, committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 8908 bytes --]

commit d8fc3b4d03aeceef9dab968395dfc346db754b27
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Oct 24 09:10:25 2019 +0100

    Define std::uniform_random_bit_generator concept for C++20
    
            * include/bits/random.h (uniform_random_bit_generator): Define for
            C++20.
            * testsuite/26_numerics/random/concept.cc: New test.
            * testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error line.

diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h
index e63dbcf5a25..270097e07e6 100644
--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -33,6 +33,9 @@
 
 #include <vector>
 #include <bits/uniform_int_dist.h>
+#if __cplusplus > 201703L
+# include <concepts>
+#endif
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -48,6 +51,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * @{
    */
 
+#ifdef __cpp_lib_concepts
+  /// Requirements for a uniform random bit generator.
+  template<typename _Gen>
+    concept uniform_random_bit_generator
+      = invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>>
+      && requires
+      {
+	{ _Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
+	{ _Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
+      };
+#endif
+
   /**
    * @brief A function template for converting the output of a (integral)
    * uniform random number generator to a floatng point result in the range
diff --git a/libstdc++-v3/testsuite/26_numerics/random/concept.cc b/libstdc++-v3/testsuite/26_numerics/random/concept.cc
new file mode 100644
index 00000000000..1794ad05419
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/random/concept.cc
@@ -0,0 +1,221 @@
+// Copyright (C) 2019 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+// { dg-require-cstdint "" }
+
+#include <random>
+
+static_assert( std::uniform_random_bit_generator<std::default_random_engine> );
+static_assert( std::uniform_random_bit_generator<std::minstd_rand0> );
+static_assert( std::uniform_random_bit_generator<std::mt19937> );
+
+struct G1
+{
+  unsigned char operator()();
+  static constexpr unsigned char min() { return 0; }
+  static constexpr unsigned char max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G1> );
+
+struct G2
+{
+  unsigned operator()();
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return -1U; }
+};
+
+static_assert( std::uniform_random_bit_generator<G2> );
+
+struct G3
+{
+  unsigned long long operator()();
+  static constexpr unsigned long long min() { return 0; }
+  static constexpr unsigned long long max() { return -1ULL; }
+};
+
+static_assert( std::uniform_random_bit_generator<G3> );
+
+struct G4
+{
+  unsigned operator()(int = 0, int = 0); // extra params, with default args
+  static constexpr unsigned min(long = 0) { return 0; }
+  static constexpr unsigned max(void* = nullptr) { return -1U; }
+};
+
+static_assert( std::uniform_random_bit_generator<G4> );
+
+struct G5
+{
+  unsigned operator()() &; // ref-qualifier
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G5> );
+
+struct G6
+{
+  unsigned operator()() const; // cv-qualifier
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G6> );
+
+struct G7
+{
+  unsigned operator()() volatile; // cv-qualifier
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G7> );
+
+struct G8
+{
+  unsigned operator()() const volatile; // cv-qualifiers
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G8> );
+
+struct G9
+{
+  unsigned operator()() const volatile; // cv-qualifiers
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G9> );
+
+struct G10
+{
+  unsigned operator()() const volatile & noexcept; // cv/ref/noexcept
+  static constexpr unsigned min() noexcept { return 0; }
+  static constexpr unsigned max() noexcept { return 10; }
+};
+
+static_assert( std::uniform_random_bit_generator<G10> );
+
+// Negative tests.
+
+static_assert( ! std::uniform_random_bit_generator<void> );
+static_assert( ! std::uniform_random_bit_generator<int> );
+static_assert( ! std::uniform_random_bit_generator<unsigned(*)()> );
+
+struct N1
+{
+  unsigned operator()();
+  constexpr unsigned min() { return 0; } // non-static
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N1> );
+
+struct N2
+{
+  unsigned operator()();
+  static constexpr unsigned min() { return 0; }
+  constexpr unsigned max() { return 1; } // non-static
+};
+
+static_assert( ! std::uniform_random_bit_generator<N2> );
+
+struct N3
+{
+  unsigned operator()();
+  // no N3::min()
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N3> );
+
+struct N4
+{
+  unsigned operator()();
+  static constexpr unsigned min() { return 0; }
+  // no N4::max()
+};
+
+static_assert( ! std::uniform_random_bit_generator<N4> );
+
+struct N5
+{
+  // no operator()
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N5> );
+
+struct N6
+{
+  int operator()(); // returns signed integral
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N6> );
+
+struct N7
+{
+  unsigned operator()();
+  static constexpr unsigned long min() { return 0; } // different return type
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N7> );
+
+struct N8
+{
+  unsigned operator()();
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned long max() { return 1; } // different return type
+};
+
+static_assert( ! std::uniform_random_bit_generator<N8> );
+
+struct N9
+{
+  unsigned operator()();
+  static constexpr unsigned long min() { return 0; } // different return type
+  static constexpr unsigned long max() { return 1; } // different return type
+};
+
+static_assert( ! std::uniform_random_bit_generator<N9> );
+
+struct N10
+{
+  unsigned operator()() &&; // ref-qualifier
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N10> );
+
+struct N11
+{
+  unsigned operator()() const &&; // ref-qualifier
+  static constexpr unsigned min() { return 0; }
+  static constexpr unsigned max() { return 1; }
+};
+
+static_assert( ! std::uniform_random_bit_generator<N11> );
diff --git a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
index f365337e789..9f7b0cec565 100644
--- a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
+++ b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
@@ -10,6 +10,6 @@ std::__detail::_Adaptor<std::mt19937, unsigned long> aurng(urng);
 auto x = std::generate_canonical<std::size_t,
 			std::numeric_limits<std::size_t>::digits>(urng);
 
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 156 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 171 }
 
 // { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3320 }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Define std::uniform_random_bit_generator concept for C++20
  2019-10-24  9:35 [PATCH] Define std::uniform_random_bit_generator concept for C++20 Jonathan Wakely
@ 2019-10-24  9:47 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2019-10-24  9:47 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 409 bytes --]

On 24/10/19 10:34 +0100, Jonathan Wakely wrote:
>	* include/bits/random.h (uniform_random_bit_generator): Define for
>	C++20.
>	* testsuite/26_numerics/random/concept.cc: New test.
>	* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error line.
>
>This is the last piece of P0898R3, "C++20 concepts library",

... so this updates the docs.

Tested powerpc64le-linux, committed to trunk.



[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 4088 bytes --]

commit bbcbcd50c07b8c735c6d19cc7487bbc736004ab7
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Oct 24 10:45:10 2019 +0100

    PR libstdc++/88338 Implement P0898R3, C++20 concepts library
    
    The implementation is already complete but this updates the docs and
    adds tests for the feature test macro.
    
            * doc/xml/manual/status_cxx2020.xml: Update status.
            * doc/html/*: Regenerate.
            * testsuite/std/concepts/1.cc: New test.
            * testsuite/std/concepts/2.cc: New test.

diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2020.xml b/libstdc++-v3/doc/xml/manual/status_cxx2020.xml
index 72c38ef985c..73949a34ad9 100644
--- a/libstdc++-v3/doc/xml/manual/status_cxx2020.xml
+++ b/libstdc++-v3/doc/xml/manual/status_cxx2020.xml
@@ -535,15 +535,14 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry>  Standard Library Concepts </entry>
       <entry>
         <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0898r3.pdf">
 	P0898R3
 	</link>
       </entry>
-      <entry align="center"> </entry>
-      <entry />
+      <entry align="center"> 10.1 </entry>
+      <entry> <code>__cpp_lib_concepts &gt;= 201806L</code> </entry>
     </row>
 
     <row>
diff --git a/libstdc++-v3/testsuite/std/concepts/1.cc b/libstdc++-v3/testsuite/std/concepts/1.cc
new file mode 100644
index 00000000000..f7f86e7b405
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/concepts/1.cc
@@ -0,0 +1,27 @@
+// Copyright (C) 2019 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do preprocess { target c++2a } }
+
+#include <concepts>
+
+#ifndef __cpp_lib_concepts
+# error "Feature test macro for concepts is missing in <concepts>"
+#elif __cpp_lib_concepts < 201806L
+# error "Feature test macro for concepts has wrong value in <concepts>"
+#endif
diff --git a/libstdc++-v3/testsuite/std/concepts/2.cc b/libstdc++-v3/testsuite/std/concepts/2.cc
new file mode 100644
index 00000000000..1b71b3110a5
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/concepts/2.cc
@@ -0,0 +1,27 @@
+// Copyright (C) 2019 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do preprocess { target c++2a } }
+
+#include <version>
+
+#ifndef __cpp_lib_concepts
+# error "Feature test macro for concepts is missing in <version>"
+#elif __cpp_lib_concepts < 201806L
+# error "Feature test macro for concepts has wrong value in <version>"
+#endif

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-24  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-24  9:35 [PATCH] Define std::uniform_random_bit_generator concept for C++20 Jonathan Wakely
2019-10-24  9:47 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).