This patch defines the __unpack_tuple_like_v support to last tuple-like
type defined in standard: ranges::subrange. In contrast to previous where
get was simple noexcept accessor, that is always defined, for subrange:
* get<0> is only defined if iterator is copyable
* get returns by value, and thus require copy/move of iterator/sentinel,
  and may throw in conseuquence.

The conditional get<0> support is resolved, by constraining the partial
sepcialization for &, const&, const&& to subrange, to only copyable
iterators.

To handle potentially throwing get, __noexcept_unpack_tuple_like_v is
define and used for noexcept specification for apply and make_from_tuple.
As list of tuple-like types are closed, and in most cases get is not-throwing,
we default it to __unpack_tuple_like_v, and specialize it for ranges::subrange
to false (get for subrange does not have noexcept specification). If noexcept
specification is added to get (is_nothrow_xxx_cosntructible), we should and
corresponding constrain with result of the trait.

As subrange is supported for apply/make_from_tuple since C++20, this
specialization are defined since that standard.

Finally, as all tuple-like types are now supported by __unpack_tuple_like_v,
the __can_make_from_tuple is replaced with __unpack_tuple_like_v of
is_constructible.

libstdc++-v3/ChangeLog:

        * include/bits/ranges_util.h (std::__unpack_tuple_like_v)
        (std::__noexcept_unpack_tuple_like_v): Define partial
        specializations for ranges::subrange.
        * include/bits/stl_pair.h (std::__noexcept_unpack_tuple_like_v):
        Define.
        * include/std/tuple (std::__can_make_from_tuple): Replace with
        __unpack_tuple_like_v<is_constructible.
        (std::apply): Use __noexcept_unpack_tuple_like_v.
        (std::make_from_tuple): Likewise and replace __can_make_from_tuple.
        * testsuite/20_util/tuple/make_from_tuple/tuple_like.cc:
        Expand test to cover noexcept.
        * testsuite/std/ranges/subrange/tuple_like2.cc: New test.
---
 libstdc++-v3/include/bits/ranges_util.h       |  35 +++
 libstdc++-v3/include/bits/stl_pair.h          |   5 +
 libstdc++-v3/include/std/tuple                |  24 +-
 .../tuple/make_from_tuple/tuple_like.cc       |  15 +-
 .../std/ranges/subrange/tuple_like2.cc        | 215 ++++++++++++++++++
 5 files changed, 274 insertions(+), 20 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc

diff --git a/libstdc++-v3/include/bits/ranges_util.h 
b/libstdc++-v3/include/bits/ranges_util.h
index 9e51aa53ec8..03335335454 100644
--- a/libstdc++-v3/include/bits/ranges_util.h
+++ b/libstdc++-v3/include/bits/ranges_util.h
@@ -484,6 +484,41 @@ namespace ranges
     inline constexpr bool __detail::__is_subrange<subrange<_Iter, _Sent, 
_Kind>> = true;
 } // namespace ranges
 
+  // Unpack a std::pair into a type trait and use its value. We also verify 
that
+  // get is supported, i.e. iterator is copyable.
+  template<template<typename...> class _Trait, typename _Tp, typename _It, 
typename _St>
+    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It, 
_St>>
+      = _Trait<_Tp, _It, _St>::value;
+
+  template<template<typename...> class _Trait, typename _Tp, copyable _It, 
typename _St>
+    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It, 
_St>&>
+      = _Trait<_Tp, _It, _St>::value;
+
+  template<template<typename...> class _Trait, typename _Tp, copyable _It, 
typename _St>
+    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const 
ranges::subrange<_It, _St>>
+      = _Trait<_Tp, _It, _St>::value;
+
+  template<template<typename...> class _Trait, typename _Tp, copyable _It, 
typename _St>
+    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const 
ranges::subrange<_It, _St>&>
+      = _Trait<_Tp, _It, _St>::value;
+
+  // Subrange get are not marked noexcept
+  template<template<typename...> class _Trait, typename _Tp, typename _It, 
typename _St>
+    constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, 
ranges::subrange<_It, _St>>
+      = false;
+
+  template<template<typename...> class _Trait, typename _Tp, typename _It, 
typename _St>
+    constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, 
ranges::subrange<_It, _St>&>
+      = false;
+
+  template<template<typename...> class _Trait, typename _Tp, typename _It, 
typename _St>
+    constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, const 
ranges::subrange<_It, _St>>
+      = false;
+
+  template<template<typename...> class _Trait, typename _Tp, typename _It, 
typename _St>
+    constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp, const 
ranges::subrange<_It, _St>&>
+      = false;
+
 #if __glibcxx_tuple_like // >= C++23
   // __is_tuple_like_v is defined in <bits/stl_pair.h>.
   template<typename _It, typename _Sent, ranges::subrange_kind _Kind>
diff --git a/libstdc++-v3/include/bits/stl_pair.h 
b/libstdc++-v3/include/bits/stl_pair.h
index b18e1b74b77..abe1ddc35b0 100644
--- a/libstdc++-v3/include/bits/stl_pair.h
+++ b/libstdc++-v3/include/bits/stl_pair.h
@@ -239,6 +239,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
     constexpr bool __unpack_tuple_like_v = false;
 
+  // The version of above that check if calls to get are noexcept.
+  // Assume it is by default.
+  template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
+    constexpr bool __noexcept_unpack_tuple_like_v = 
__unpack_tuple_like_v<_Trait, _Tp, _Tuple>;
+
   // Unpack a std::tuple into a type trait and use its value.
   template<template<typename...> class _Trait, typename _Tp, typename... _Up>
     constexpr bool __unpack_tuple_like_v<_Trait, _Tp, tuple<_Up...>>
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index 7d6039ceb38..fd89f6d2540 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -2928,7 +2928,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
     constexpr decltype(auto)
     apply(_Fn&& __f, _Tuple&& __t)
-    noexcept(__unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>)
+    noexcept(__noexcept_unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>)
     {
       using _Indices
        = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
@@ -2939,25 +2939,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
 
 #ifdef __cpp_lib_make_from_tuple // C++ >= 17
-  template <typename _Tp, typename _Tuple, typename _Seq
-             = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>>
-    constexpr bool __can_make_from_tuple = false;
-
-  // _GLIBCXX_RESOLVE_LIB_DEFECTS
-  // 3528. make_from_tuple can perform (the equivalent of) a C-style cast
-  template <typename _Tp, typename _Tuple, size_t... _Idx>
-    constexpr bool __can_make_from_tuple<_Tp, _Tuple, index_sequence<_Idx...>>
-      = is_constructible_v<_Tp,
-                          decltype(std::get<_Idx>(std::declval<_Tuple>()))...>;
-
   template <typename _Tp, typename _Tuple, size_t... _Idx>
     constexpr _Tp
     __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
     {
-      static_assert(__can_make_from_tuple<_Tp, _Tuple, 
index_sequence<_Idx...>>);
+      static_assert(__unpack_tuple_like_v<is_constructible, _Tp, _Tuple>);
       return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...);
     }
 
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 3528. make_from_tuple can perform (the equivalent of) a C-style cast
+
 #if __cpp_lib_tuple_like // >= C++23
   template <typename _Tp, __tuple_like _Tuple>
 #else
@@ -2965,12 +2957,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
     constexpr auto
     make_from_tuple(_Tuple&& __t)
-    noexcept(__unpack_tuple_like_v<is_nothrow_constructible, _Tp, _Tuple>)
+    noexcept(__noexcept_unpack_tuple_like_v<is_nothrow_constructible, _Tp, 
_Tuple>)
 #ifdef __cpp_concepts // >= C++20
     -> _Tp
-    requires __can_make_from_tuple<_Tp, _Tuple>
+    requires __unpack_tuple_like_v<is_constructible, _Tp, _Tuple>
 #else
-    -> __enable_if_t<__can_make_from_tuple<_Tp, _Tuple>, _Tp>
+    -> __enable_if_t<__unpack_tuple_like_v<is_constructible, _Tp, _Tuple>, _Tp>
 #endif
     {
       constexpr size_t __n = tuple_size_v<remove_reference_t<_Tuple>>;
diff --git a/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc 
b/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
index 91adea29fd9..be32af6cc5c 100644
--- a/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
+++ b/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
@@ -13,6 +13,7 @@ constexpr bool can_make_from_tuple<Dest, Tuple, 
decltype(void(std::make_from_tup
 struct Two
 {
   Two(const char*, int) noexcept;
+  Two(int, int);
 };
 
 void
@@ -22,13 +23,16 @@ test_pair()
   auto two = std::make_from_tuple<Two>(p);
   static_assert(std::is_same_v<decltype(two), Two>, "make from pair");
   static_assert(noexcept(std::make_from_tuple<Two>(p)), "noexcept for pair");
-  static_assert(!can_make_from_tuple<Two, std::pair<long, long>>);
-}
+  static_assert(!can_make_from_tuple<Two, std::pair<int, const char*>>);
 
+  std::pair p2(10, 10);
+  static_assert(!noexcept(std::make_from_tuple<Two>(p2)), "noexcept for pair");
+}
 
 struct Three
 {
   Three(int, int, int) noexcept;
+  Three(int*, int*, int*);
 };
 
 void
@@ -39,7 +43,10 @@ test_array()
   static_assert(std::is_same_v<decltype(three), Three>, "make from array");
   static_assert(noexcept(std::make_from_tuple<Three>(a)), "noexcept for 
array");
   static_assert(!can_make_from_tuple<Three, std::array<int, 2>>);
-  static_assert(!can_make_from_tuple<Three, std::array<int*, 3>>);
+  static_assert(!can_make_from_tuple<Three, std::array<char*, 3>>);
+
+  std::array<int*, 3> a2{{&a[0], &a[1], &a[2]}};
+  static_assert(!noexcept(std::make_from_tuple<Three>(a2)), "noexcept for 
array");
 }
 
 #if __cplusplus >= 202002L
@@ -60,10 +67,10 @@ test_subrange() // PR libstdc++/102301
   std::ranges::subrange sr(r);
   auto v = std::make_from_tuple<std::vector<int>>(sr);
   static_assert(std::is_same_v<decltype(v), std::vector<int>>, "from 
subrange");
+  static_assert(!noexcept(std::make_from_tuple<std::vector<int>>(sr)), 
"noexcept for subrange");
 
   auto cont = std::make_from_tuple<Cont>(sr);
   static_assert(std::is_same_v<decltype(cont), Cont>, "from subrange");
-  // TODO fix
   static_assert(!noexcept(std::make_from_tuple<Cont>(sr)), "noexcept for 
subrange");
   static_assert(!can_make_from_tuple<Cont, 
std::ranges::subrange<std::istream_iterator<int>>>);
 }
diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc 
b/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
new file mode 100644
index 00000000000..614e690d0e5
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
@@ -0,0 +1,215 @@
+// { dg-do compile { target c++26 } }
+
+#include <ranges>
+#include <algorithm>
+#include <string_view>
+#include <type_traits>
+#include <tuple>
+#include <utility>
+
+#include <testsuite_hooks.h>
+
+template <typename It, typename St>
+constexpr void
+test_sanity()
+{
+  using R = std::ranges::subrange<It, St>;
+
+  static_assert(std::tuple_size_v<R> == 2);
+  static_assert(std::is_same_v<std::tuple_element_t<0, R>, It>);
+  static_assert(std::is_same_v<std::tuple_element_t<1, R>, St>);
+
+  static_assert(std::is_same_v<decltype(std::get<0>(std::declval<R>())), It>);
+  if constexpr (std::copyable<It>)
+  {
+    static_assert(std::is_same_v<decltype(std::get<0>(std::declval<R&>())), 
It>);
+    static_assert(std::is_same_v<decltype(std::get<0>(std::declval<const 
R>())), It>);
+    static_assert(std::is_same_v<decltype(std::get<0>(std::declval<const 
R&>())), It>);
+  }
+
+  static_assert(std::is_same_v<decltype(std::get<1>(std::declval<R&>())), St>);
+  static_assert(std::is_same_v<decltype(std::get<1>(std::declval<R>())), St>);
+  static_assert(std::is_same_v<decltype(std::get<1>(std::declval<const R>())), 
St>);
+  static_assert(std::is_same_v<decltype(std::get<1>(std::declval<const 
R&>())), St>);
+}
+
+template <typename It, typename St>
+constexpr void
+test_get()
+{
+  using R = std::ranges::subrange<It, St>;
+
+  R rng{It(), St()};
+
+  if constexpr (std::copyable<It>)
+  {
+    const R crng{It(), St()};
+    VERIFY(std::get<0>(rng) == std::get<1>(rng));
+    VERIFY(std::get<0>(crng) == std::get<1>(crng));
+  }
+  VERIFY(std::get<0>(std::move(rng)) == std::get<1>(std::move(rng)));
+}
+
+template <typename It, typename St>
+constexpr void
+test_structured_binding()
+{
+  using R = std::ranges::subrange<It, St>;
+  R rng{It(), St()};
+
+  if constexpr (std::copyable<It>)
+  {
+    auto& [b1, e1] = rng;
+    VERIFY( b1 == e1 );
+
+    const R crng{It(), St()};
+    auto& [b2, e2] = crng;
+    VERIFY( b2 == e2 );
+  }
+
+  auto [b, e] = std::move(rng);
+  VERIFY( b == e );
+}
+
+template <typename It, typename St>
+constexpr void
+test_tuple_cat()
+{
+  std::ranges::subrange<It, St> rng{It(), St()};
+  std::pair<int, std::string_view> p(42, "hello");
+
+  auto r = std::tuple_cat(std::move(rng), p);
+  static_assert(std::is_same_v<decltype(r), std::tuple<It, St, int, 
std::string_view>>);
+  VERIFY( std::get<0>(r) == std::get<1>(r) );
+  VERIFY( std::get<2>(r) == 42 );
+  VERIFY( std::get<3>(r) == "hello" );
+}
+
+template<typename Dest, typename Tuple>
+concept can_make_from_tuple = requires (Tuple t)
+{ std::make_from_tuple<Dest>(std::forward<Tuple>(t)); };
+
+struct Cont
+{
+  template<typename T, typename U>
+  constexpr Cont(const T&, const U&) noexcept
+  { }
+};
+
+template<typename Rng>
+constexpr bool noexcept_get
+  = noexcept(std::get<0>(std::declval<Rng>())) && 
noexcept(std::get<1>(std::declval<Rng>()));
+
+template <typename It, typename St>
+constexpr void
+test_apply()
+{
+  using R = std::ranges::subrange<It, St>;
+  R rng{It(), St()};
+  const R& crng = rng;
+
+  auto f = [](It b, St e) noexcept { return b == e; };
+
+  if constexpr (std::copyable<It>)
+  {
+    auto r1 = std::apply(f, rng);
+    VERIFY( r1 );
+    static_assert( noexcept(std::apply(f, rng)) == noexcept_get<R&> );
+
+    auto r2 = std::apply(f, crng);
+    VERIFY( r2 );
+    static_assert( noexcept(std::apply(f, crng)) == noexcept_get<const R&> );
+  }
+
+  auto r = std::apply(f, std::move(rng));
+  VERIFY( r );
+  static_assert( noexcept(std::apply(f, std::move(rng))) == noexcept_get<R> );
+
+  if constexpr (std::copyable<It>)
+  {
+    auto p1 = std::make_from_tuple<Cont>(crng);
+    static_assert( noexcept(std::make_from_tuple<Cont>(rng)) == 
noexcept_get<R&> );
+
+    auto p2 = std::make_from_tuple<Cont>(crng);
+    static_assert( noexcept(std::make_from_tuple<Cont>(rng)) == 
noexcept_get<const R&> );
+  }
+  else
+  {
+    static_assert(!can_make_from_tuple<Cont, R&>);
+    static_assert(!can_make_from_tuple<Cont, const R&>);
+  }
+
+  auto p = std::make_from_tuple<Cont>(std::move(rng));
+  static_assert( noexcept(std::make_from_tuple<Cont>(std::move(rng))) == 
noexcept_get<R> );
+
+  static_assert(!can_make_from_tuple<std::pair<int, int>, R>);
+  static_assert(!can_make_from_tuple<std::pair<int, int>, R&>);
+  static_assert(!can_make_from_tuple<std::pair<int, int>, const R&>);
+}
+
+template <typename It, typename St>
+constexpr bool
+test_all()
+{
+  test_sanity<It, St>();
+  test_structured_binding<It, St>();
+  test_tuple_cat<It, St>();
+  test_apply<It, St>();
+  test_get<It, St>();
+  return true;
+}
+
+template<int Move, int Copy>
+struct Iterator
+{
+  using iterator_concept = std::input_iterator_tag;
+  using value_type       = int;
+  using difference_type  = std::ptrdiff_t;
+
+  constexpr Iterator() = default;
+
+  constexpr Iterator(Iterator&&) noexcept(Move > 1)
+    requires (Move > 0)
+  { }
+
+  constexpr Iterator(const Iterator&) noexcept(Copy > 1)
+    requires (Copy > 0)
+  { }
+
+  constexpr Iterator& operator=(Iterator&&) noexcept(Move > 1)
+    requires (Move > 0)
+  { return *this; }
+
+  constexpr Iterator& operator=(const Iterator&) noexcept(Copy > 1)
+    requires (Copy > 0)
+  { return *this; }
+
+  constexpr int
+  operator*() const
+  { return 10; }
+
+  constexpr Iterator&
+  operator++()
+  { return *this; }
+
+  constexpr void
+  operator++(int)
+  { }
+
+  friend constexpr bool
+  operator==(const Iterator&, const Iterator&)
+  { return true; }
+
+  friend constexpr bool
+  operator==(const Iterator&, std::default_sentinel_t)
+  { return true; }
+};
+
+static_assert( test_all<Iterator<1, 0>, std::default_sentinel_t>() );
+static_assert( test_all<Iterator<2, 0>, std::default_sentinel_t>() );
+static_assert( test_all<Iterator<1, 1>, std::default_sentinel_t>() );
+static_assert( test_all<Iterator<2, 1>, std::default_sentinel_t>() );
+static_assert( test_all<Iterator<1, 1>, Iterator<1, 1>>() );
+static_assert( test_all<Iterator<2, 1>, Iterator<2, 1>>() );
+static_assert( test_all<Iterator<2, 2>, Iterator<2, 2>>() );
+
-- 
2.56.0

Reply via email to