]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Implement P2968R2 "Making std::ignore a first-class object"
authorJonathan Wakely <jwakely@redhat.com>
Thu, 25 Jul 2024 12:00:09 +0000 (13:00 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 25 Jul 2024 22:10:35 +0000 (23:10 +0100)
This was recently approved for C++26, but we can apply the changes for
all modes back to C++11. There's no reason not to make the assignment
usable in constant expressions for C++11 mode, and noexcept for all
modes.

Move the definitions to <bits/utility.h> so they're available in
<utility> as well as <tuple>.

libstdc++-v3/ChangeLog:

* include/bits/utility.h (_Swallow_assign): Make assignment
constexpr for C++11 as well, and add noexcept.
* include/std/tuple (_Swallow_assign, ignore): Move to
bits/utility.h.
* testsuite/20_util/headers/utility/ignore.cc: New test.

libstdc++-v3/include/bits/utility.h
libstdc++-v3/include/std/tuple
libstdc++-v3/testsuite/20_util/headers/utility/ignore.cc [new file with mode: 0644]

index 9f3b99231b3c5ed0d3051d1ab0b3ef69bac5239c..44c74333e92433b4297e53ae384f684702a87a77 100644 (file)
@@ -280,6 +280,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   } // namespace __detail
 #endif
 
+  // A class (and instance) which can be used in 'tie' when an element
+  // of a tuple is not required.
+  struct _Swallow_assign
+  {
+    template<class _Tp>
+      constexpr const _Swallow_assign&
+      operator=(const _Tp&) const noexcept
+      { return *this; }
+  };
+
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 2773. Making std::ignore constexpr
+  /** Used with `std::tie` to ignore an element of a tuple
+   *
+   * When using `std::tie` to assign the elements of a tuple to variables,
+   * unwanted elements can be ignored by using `std::ignore`. For example:
+   *
+   * ```
+   * int x, y;
+   * std::tie(x, std::ignore, y) = std::make_tuple(1, 2, 3);
+   * ```
+   *
+   * This assignment will perform `x=1; std::ignore=2; y=3;` which results
+   * in the second element being ignored.
+   *
+   * @since C++11
+   */
+  _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 
index df3f6e38eebfdeeee8bc4573533347a41a584d88..93b649e7d21109e2c91a2b02c8513e76f454dde3 100644 (file)
@@ -2845,37 +2845,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
 #endif
 
-  // A class (and instance) which can be used in 'tie' when an element
-  // of a tuple is not required.
-  // _GLIBCXX14_CONSTEXPR
-  // 2933. PR for LWG 2773 could be clearer
-  struct _Swallow_assign
-  {
-    template<class _Tp>
-      _GLIBCXX14_CONSTEXPR const _Swallow_assign&
-      operator=(const _Tp&) const
-      { return *this; }
-  };
-
-  // _GLIBCXX_RESOLVE_LIB_DEFECTS
-  // 2773. Making std::ignore constexpr
-  /** Used with `std::tie` to ignore an element of a tuple
-   *
-   * When using `std::tie` to assign the elements of a tuple to variables,
-   * unwanted elements can be ignored by using `std::ignore`. For example:
-   *
-   * ```
-   * int x, y;
-   * std::tie(x, std::ignore, y) = std::make_tuple(1, 2, 3);
-   * ```
-   *
-   * This assignment will perform `x=1; std::ignore=2; y=3;` which results
-   * in the second element being ignored.
-   *
-   * @since C++11
-   */
-  _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
-
   /// Partial specialization for tuples
   template<typename... _Types, typename _Alloc>
     struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
diff --git a/libstdc++-v3/testsuite/20_util/headers/utility/ignore.cc b/libstdc++-v3/testsuite/20_util/headers/utility/ignore.cc
new file mode 100644 (file)
index 0000000..fc7a45d
--- /dev/null
@@ -0,0 +1,29 @@
+// { dg-do compile { target c++11 } }
+
+// P2968R2 Make std::ignore a first-class object.
+// This is a C++26 change, but we treat it as a DR against C++11.
+
+// C++26 [tuple.general]:
+// In addition to being available via inclusion of the <tuple> header,
+// ignore is available when <utility> is included.
+#include <utility>
+
+using ignore_type = std::remove_const<decltype(std::ignore)>::type;
+
+#ifdef __cpp_lib_is_aggregate
+static_assert( std::is_aggregate_v<ignore_type> );
+#endif
+
+static_assert( std::is_nothrow_default_constructible<ignore_type>::value, "" );
+static_assert( std::is_nothrow_copy_constructible<ignore_type>::value, "" );
+static_assert( std::is_nothrow_copy_assignable<ignore_type>::value, "" );
+
+static_assert( std::is_nothrow_assignable<const ignore_type&, int>::value,
+    "assignable from arbitrary types" );
+static_assert( std::is_nothrow_assignable<const ignore_type&, long*>::value,
+    "assignable from arbitrary types" );
+
+constexpr ignore_type ignore;
+constexpr ignore_type ignore_more(ignore);
+constexpr ignore_type ignore_morer(ignore = ignore);
+constexpr ignore_type ignore_morest(ignore = "");
This page took 0.107437 seconds and 5 git commands to generate.