xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.elements/iterator/deref.pass.cpp (revision b2cc4b994e5fb85053b1acedec5ea0d1d42e5ec4)
194461822SHui Xie //===----------------------------------------------------------------------===//
294461822SHui Xie //
394461822SHui Xie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
494461822SHui Xie // See https://llvm.org/LICENSE.txt for license information.
594461822SHui Xie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
694461822SHui Xie //
794461822SHui Xie //===----------------------------------------------------------------------===//
894461822SHui Xie 
994461822SHui Xie // UNSUPPORTED: c++03, c++11, c++14, c++17
1094461822SHui Xie 
1194461822SHui Xie // constexpr decltype(auto) operator*() const;
1294461822SHui Xie 
1394461822SHui Xie #include <array>
1494461822SHui Xie #include <cassert>
1594461822SHui Xie #include <ranges>
1694461822SHui Xie #include <tuple>
1794461822SHui Xie #include <utility>
1894461822SHui Xie 
1994461822SHui Xie template <std::size_t N, class T, std::size_t Size>
testReference(T (& ts)[Size])2094461822SHui Xie constexpr void testReference(T (&ts)[Size]) {
2194461822SHui Xie   auto ev = ts | std::views::elements<N>;
2294461822SHui Xie   auto it = ev.begin();
2394461822SHui Xie 
2494461822SHui Xie   decltype(auto) result = *it;
2594461822SHui Xie   using ExpectedType    = decltype(std::get<N>(ts[0]));
2694461822SHui Xie   static_assert(std::is_same_v<decltype(result), ExpectedType>);
2794461822SHui Xie 
2894461822SHui Xie   if constexpr (std::is_reference_v<ExpectedType>) {
2994461822SHui Xie     // tuple/array/pair
3094461822SHui Xie     assert(&result == &std::get<N>(ts[0]));
3194461822SHui Xie   } else {
3294461822SHui Xie     // subrange
3394461822SHui Xie     assert(result == std::get<N>(ts[0]));
3494461822SHui Xie   }
3594461822SHui Xie }
3694461822SHui Xie 
3794461822SHui Xie // LWG 3502 elements_view should not be allowed to return dangling references
3894461822SHui Xie template <std::size_t N, class T>
testValue(T t)3994461822SHui Xie constexpr void testValue(T t) {
4094461822SHui Xie   auto ev = std::views::iota(0, 1) | std::views::transform([&t](int) { return t; }) | std::views::elements<N>;
4194461822SHui Xie   auto it = ev.begin();
4294461822SHui Xie 
4394461822SHui Xie   decltype(auto) result = *it;
4494461822SHui Xie   using ExpectedType    = std::remove_cvref_t<decltype(std::get<N>(t))>;
4594461822SHui Xie   static_assert(std::is_same_v<decltype(result), ExpectedType>);
4694461822SHui Xie 
4794461822SHui Xie   assert(result == std::get<N>(t));
4894461822SHui Xie }
4994461822SHui Xie 
test()5094461822SHui Xie constexpr bool test() {
5194461822SHui Xie   // test tuple
5294461822SHui Xie   {
53*b2cc4b99SStephan T. Lavavej     std::tuple<int, short, long> ts[] = {{1, short{2}, 3}, {4, short{5}, 6}};
5494461822SHui Xie     testReference<0>(ts);
5594461822SHui Xie     testReference<1>(ts);
5694461822SHui Xie     testReference<2>(ts);
5794461822SHui Xie     testValue<0>(ts[0]);
5894461822SHui Xie     testValue<1>(ts[0]);
5994461822SHui Xie     testValue<2>(ts[0]);
6094461822SHui Xie   }
6194461822SHui Xie 
6294461822SHui Xie   // test pair
6394461822SHui Xie   {
64*b2cc4b99SStephan T. Lavavej     std::pair<int, short> ps[] = {{1, short{2}}, {4, short{5}}};
6594461822SHui Xie     testReference<0>(ps);
6694461822SHui Xie     testReference<1>(ps);
6794461822SHui Xie     testValue<0>(ps[0]);
6894461822SHui Xie     testValue<1>(ps[0]);
6994461822SHui Xie   }
7094461822SHui Xie 
7194461822SHui Xie   // test array
7294461822SHui Xie   {
7394461822SHui Xie     std::array<int, 3> arrs[] = {{1, 2, 3}, {3, 4, 5}};
7494461822SHui Xie     testReference<0>(arrs);
7594461822SHui Xie     testReference<1>(arrs);
7694461822SHui Xie     testReference<2>(arrs);
7794461822SHui Xie     testValue<0>(arrs[0]);
7894461822SHui Xie     testValue<1>(arrs[0]);
7994461822SHui Xie     testValue<2>(arrs[0]);
8094461822SHui Xie   }
8194461822SHui Xie 
8294461822SHui Xie   // test subrange
8394461822SHui Xie   {
8494461822SHui Xie     int i                             = 5;
8594461822SHui Xie     std::ranges::subrange<int*> srs[] = {{&i, &i}, {&i, &i}};
8694461822SHui Xie     testReference<0>(srs);
8794461822SHui Xie     testReference<1>(srs);
8894461822SHui Xie     testValue<0>(srs[0]);
8994461822SHui Xie     testValue<1>(srs[0]);
9094461822SHui Xie   }
9194461822SHui Xie 
9294461822SHui Xie   return true;
9394461822SHui Xie }
9494461822SHui Xie 
main(int,char **)9594461822SHui Xie int main(int, char**) {
9694461822SHui Xie   test();
9794461822SHui Xie   static_assert(test());
9894461822SHui Xie 
9994461822SHui Xie   return 0;
10094461822SHui Xie }
101