[libcxx-commits] [libcxx] [libc++][ranges] implement `ranges::elements_of` (PR #91414)

Xiaoyang Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 8 19:49:05 PDT 2024


================
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, c++20
+
+// std::ranges::elements_of;
+
+#include <ranges>
+
+#include <concepts>
+#include <memory>
+#include <vector>
+
+constexpr bool test() {
+  {
+    auto elements_of = std::ranges::elements_of(std::vector<int>());
+    static_assert(
+        std::same_as<decltype(elements_of), std::ranges::elements_of<std::vector<int>&&, std::allocator<std::byte>>>);
+    static_assert(std::same_as<decltype(elements_of.range), std::vector<int>&&>);
+    static_assert(std::same_as<decltype(elements_of.allocator), std::allocator<std::byte>>);
+  }
+  {
+    auto elements_of = std::ranges::elements_of(std::vector<int>(), std::allocator<int>());
+    static_assert(
+        std::same_as<decltype(elements_of), std::ranges::elements_of<std::vector<int>&&, std::allocator<int>>>);
+    static_assert(std::same_as<decltype(elements_of.range), std::vector<int>&&>);
+    static_assert(std::same_as<decltype(elements_of.allocator), std::allocator<int>>);
+  }
+  return true;
+}
----------------
xiaoyang-sde wrote:

This test fails with AppleClang 15 due to the deduced type for `elements_of.range` being `std::vector<int>` instead of the expected `std::vector<int&&>`. Removing the constraint on the template parameter enables the test to pass:

```diff
- template <range _Range, class _Allocator = allocator<byte>>
+ template <class _Range, class _Allocator = allocator<byte>>
struct elements_of {
  _LIBCPP_NO_UNIQUE_ADDRESS _Range range;
  _LIBCPP_NO_UNIQUE_ADDRESS _Allocator allocator;

  // This explicit constructor is required because AppleClang 15 hasn't implement P0960R3
  _LIBCPP_HIDE_FROM_ABI explicit constexpr elements_of(_Range __range, _Allocator __alloc = _Allocator())
      : range(std::move(__range)), allocator(std::move(__alloc)) {}
};
```

https://github.com/llvm/llvm-project/pull/91414


More information about the libcxx-commits mailing list