[libcxx-commits] [libcxx] f280505 - [libcxx] adds `std::indirectly_readable_traits` to <iterator>

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 15 16:59:53 PDT 2021


Author: Christopher Di Bella
Date: 2021-04-15T23:59:02Z
New Revision: f280505aa08432f5d4c4296122c6ff728b753398

URL: https://github.com/llvm/llvm-project/commit/f280505aa08432f5d4c4296122c6ff728b753398
DIFF: https://github.com/llvm/llvm-project/commit/f280505aa08432f5d4c4296122c6ff728b753398.diff

LOG: [libcxx] adds `std::indirectly_readable_traits` to <iterator>

Implements parts of:
    * P0896R4 The One Ranges Proposal
    * LWG3446 `indirectly_readable_traits` ambiguity for types with both `value_type` and `element_type`

Depends on D99141.

Differential Revision: https://reviews.llvm.org/D99461

Added: 
    libcxx/test/std/iterators/iterator.requirements/iterator.assoc.types/readable.traits/indirectly_readable_traits.compile.pass.cpp

Modified: 
    libcxx/docs/Cxx2aStatusIssuesStatus.csv
    libcxx/include/iterator

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Cxx2aStatusIssuesStatus.csv b/libcxx/docs/Cxx2aStatusIssuesStatus.csv
index 3967999a65dd..a471cd4f8b0a 100644
--- a/libcxx/docs/Cxx2aStatusIssuesStatus.csv
+++ b/libcxx/docs/Cxx2aStatusIssuesStatus.csv
@@ -297,3 +297,4 @@
 "`3396 <https://wg21.link/LWG3396>`__","Clarify point of reference for ``source_location::current()``\  (DE 169)","Prague","",""
 "`3397 <https://wg21.link/LWG3397>`__","``ranges::basic_istream_view::iterator``\  should not provide ``iterator_category``\ ","Prague","",""
 "`3398 <https://wg21.link/LWG3398>`__","``tuple_element_t``\  is also wrong for ``const subrange``\ ","Prague","",""
+"`3446 <https://wg21.link/LWG3446>`__","``indirectly_readable_traits``\ ambiguity for types with both ``value_type``\ and ``element_type``\ ","November virtual meeting","|Complete|","13.0"

diff  --git a/libcxx/include/iterator b/libcxx/include/iterator
index 4e1077f7f089..cc2b26400fba 100644
--- a/libcxx/include/iterator
+++ b/libcxx/include/iterator
@@ -17,7 +17,8 @@
 
 namespace std
 {
-template<class> struct incrementable_traits; // since C++20
+template<class> struct incrementable_traits;       // since C++20
+template<class> struct indirectly_readable_traits; // since C++20
 
 template<class Iterator>
 struct iterator_traits
@@ -472,6 +473,53 @@ struct incrementable_traits<_Tp> {
 };
 
 // TODO(cjdb): add iter_
diff erence_t once iterator_traits is cleaned up.
+
+// [readable.traits]
+template<class> struct __cond_value_type {};
+
+template<class _Tp>
+requires is_object_v<_Tp>
+struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
+
+template<class _Tp>
+concept __has_member_value_type = requires { typename _Tp::value_type; };
+
+template<class _Tp>
+concept __has_member_element_type = requires { typename _Tp::element_type; };
+
+template<class> struct indirectly_readable_traits {};
+
+template<class _Ip>
+requires is_array_v<_Ip>
+struct indirectly_readable_traits<_Ip> {
+  using value_type = remove_cv_t<remove_extent_t<_Ip>>;
+};
+
+template<class _Ip>
+struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
+
+template<class _Tp>
+struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
+
+template<__has_member_value_type _Tp>
+struct indirectly_readable_traits<_Tp>
+  : __cond_value_type<typename _Tp::value_type> {};
+
+template<__has_member_element_type _Tp>
+struct indirectly_readable_traits<_Tp>
+  : __cond_value_type<typename _Tp::element_type> {};
+
+// Pre-emptively applies LWG3541
+template<__has_member_value_type _Tp>
+requires __has_member_element_type<_Tp>
+struct indirectly_readable_traits<_Tp> {};
+template<__has_member_value_type _Tp>
+requires __has_member_element_type<_Tp> &&
+         same_as<remove_cv_t<typename _Tp::element_type>,
+                 remove_cv_t<typename _Tp::value_type>>
+struct indirectly_readable_traits<_Tp>
+  : __cond_value_type<typename _Tp::value_type> {};
+
 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
 
 template <class _Iter>

diff  --git a/libcxx/test/std/iterators/iterator.requirements/iterator.assoc.types/readable.traits/indirectly_readable_traits.compile.pass.cpp b/libcxx/test/std/iterators/iterator.requirements/iterator.assoc.types/readable.traits/indirectly_readable_traits.compile.pass.cpp
new file mode 100644
index 000000000000..6cab7cf25319
--- /dev/null
+++ b/libcxx/test/std/iterators/iterator.requirements/iterator.assoc.types/readable.traits/indirectly_readable_traits.compile.pass.cpp
@@ -0,0 +1,187 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+
+// template<class T>
+// struct indirectly_readable_traits;
+
+#include <iterator>
+
+#include <concepts>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+// `value_type` and `element_type` member aliases aren't actually used to declare anytihng, so GCC
+// thinks they're completely unused.
+#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
+
+// clang-format off
+template <class T>
+concept check_has_value_type = requires {
+  typename std::indirectly_readable_traits<T>::value_type;
+};
+
+template <class T, class Expected>
+concept check_value_type_matches =
+  check_has_value_type<T> &&
+  std::same_as<typename std::indirectly_readable_traits<T>::value_type, Expected>;
+// clang-format on
+
+template <class T>
+constexpr bool check_pointer() {
+  constexpr bool result = check_value_type_matches<T*, T>;
+  static_assert(check_value_type_matches<T const*, T> == result);
+  static_assert(check_value_type_matches<T volatile*, T> == result);
+  static_assert(check_value_type_matches<T const volatile*, T> == result);
+
+  static_assert(check_value_type_matches<T* const, T> == result);
+  static_assert(check_value_type_matches<T const* const, T> == result);
+  static_assert(check_value_type_matches<T volatile* const, T> == result);
+  static_assert(check_value_type_matches<T const volatile* const, T> == result);
+
+  return result;
+}
+
+static_assert(!check_pointer<void>());
+static_assert(check_pointer<int>());
+static_assert(check_pointer<long>());
+static_assert(check_pointer<double>());
+static_assert(check_pointer<double*>());
+
+struct S {};
+static_assert(check_pointer<S>());
+
+template <class T>
+constexpr bool check_array() {
+  constexpr bool result = check_value_type_matches<T[], T>;
+  static_assert(check_value_type_matches<T const[], T> == result);
+  static_assert(check_value_type_matches<T volatile[], T> == result);
+  static_assert(check_value_type_matches<T const volatile[], T> == result);
+  static_assert(check_value_type_matches<T[10], T> == result);
+  static_assert(check_value_type_matches<T const[10], T> == result);
+  static_assert(check_value_type_matches<T volatile[10], T> == result);
+  static_assert(check_value_type_matches<T const volatile[10], T> == result);
+  return result;
+}
+
+static_assert(check_array<int>());
+static_assert(check_array<long>());
+static_assert(check_array<double>());
+static_assert(check_array<double*>());
+static_assert(check_array<S>());
+
+template <class T, class Expected>
+constexpr bool check_explicit_member() {
+  constexpr bool result = check_value_type_matches<T, Expected>;
+  static_assert(check_value_type_matches<T const, Expected> == result);
+  return result;
+}
+
+struct has_value_type {
+  using value_type = int;
+};
+static_assert(check_explicit_member<has_value_type, int>());
+static_assert(check_explicit_member<std::vector<int>::iterator, int>());
+
+struct has_element_type {
+  using element_type = S;
+};
+static_assert(check_explicit_member<has_element_type, S>());
+
+struct has_same_value_and_element_type {
+  using value_type = int;
+  using element_type = int;
+};
+static_assert(check_explicit_member<has_same_value_and_element_type, int>());
+static_assert(check_explicit_member<std::shared_ptr<long>, long>());
+static_assert(check_explicit_member<std::shared_ptr<long const>, long>());
+
+// clang-format off
+template<class T, class U>
+requires std::same_as<std::remove_cv_t<T>, std::remove_cv_t<U> >
+struct possibly_
diff erent_cv_qualifiers {
+  using value_type = T;
+  using element_type = U;
+};
+// clang-format on
+
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int, int>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int, int const>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int, int volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int, int const volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const, int>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const, int const>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const, int volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const, int const volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int volatile, int>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int volatile, int const>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int volatile, int volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int volatile, int const volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const volatile, int>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const volatile, int const>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const volatile, int volatile>, int>());
+static_assert(check_explicit_member<possibly_
diff erent_cv_qualifiers<int const volatile, int const volatile>, int>());
+
+struct S2 {};
+namespace std {
+template <>
+struct indirectly_readable_traits<S2> {
+  using value_type = int;
+};
+} // namespace std
+static_assert(check_value_type_matches<S2, int>);
+static_assert(check_value_type_matches<std::vector<int>, int>);
+static_assert(check_value_type_matches<std::vector<int>::iterator, int>);
+static_assert(check_value_type_matches<std::vector<int>::const_iterator, int>);
+static_assert(check_value_type_matches<std::istream_iterator<int>, int>);
+static_assert(check_value_type_matches<std::istreambuf_iterator<char>, char>);
+static_assert(check_value_type_matches<std::optional<int>, int>);
+
+template <class T>
+constexpr bool check_ref() {
+  struct ref_value {
+    using value_type = T&;
+  };
+  constexpr bool result = check_has_value_type<ref_value>;
+
+  struct ref_element {
+    using element_type = T&;
+  };
+  static_assert(check_has_value_type<ref_element> == result);
+
+  return result;
+}
+
+static_assert(!check_ref<int>());
+static_assert(!check_ref<S>());
+static_assert(!check_ref<std::shared_ptr<long> >());
+
+static_assert(!check_has_value_type<void>);
+static_assert(!check_has_value_type<std::nullptr_t>);
+static_assert(!check_has_value_type<int>);
+static_assert(!check_has_value_type<S>);
+
+struct has_
diff erent_value_and_element_type {
+  using value_type = int;
+  using element_type = long;
+};
+static_assert(!check_has_value_type<has_
diff erent_value_and_element_type>);
+
+struct void_value {
+  using value_type = void;
+};
+static_assert(!check_has_value_type<void_value>);
+
+struct void_element {
+  using element_type = void;
+};
+static_assert(!check_has_value_type<void_element>);


        


More information about the libcxx-commits mailing list