// { dg-do run { target c++23 } } #include #if __cpp_lib_containers_ranges != 202202L # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in " #endif #include #include #include #include #include #include #include struct StateCmp { int state = 7; template bool operator()(T const& l, U const & r) const { return l > r; } }; void test_deduction_guide() { __gnu_test::test_input_range> r(0, 0); std::multimap m(std::from_range, r); static_assert(std::is_same_v>); StateCmp cmp; std::multimap m2(std::from_range, r, cmp); static_assert(std::is_same_v>); using Alloc = __gnu_test::SimpleAllocator>; Alloc alloc; std::multimap m3(std::from_range, r, alloc); static_assert(std::is_same_v, Alloc>>); std::multimap m4(std::from_range, r, cmp, alloc); static_assert(std::is_same_v>); __gnu_test::test_input_range> r2(0, 0); std::multimap m5(std::from_range, r2); static_assert(std::is_same_v>); // LWG4223: deduces multimap //__gnu_test::test_input_range> r3(0, 0); // std::multimap m6(std::from_range, r3); __gnu_test::test_input_range> r4(0, 0); std::multimap m7(std::from_range, r4); static_assert(std::is_same_v>); std::multimap it7(r4.begin(), r4.begin()); static_assert(std::is_same_v>); } template constexpr bool is_equal(std::less, std::less) { return true; } constexpr bool is_equal(StateCmp lhs, StateCmp rhs) { return lhs.state = rhs.state; } constexpr auto get0 = [](auto const& t) { using std::get; return get<0>(t); }; template constexpr void do_test(Alloc alloc, Cmp cmp) { // The multimap's value_type, key_type and multimapped_type. using P = typename Alloc::value_type; using K = typename P::first_type; using V = typename P::second_type; // The range's value_type. using T = std::ranges::range_value_t; T a[]{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,0}, {1,1},{2,2},{3,3},{4,4},{5,5}}; auto eq = [&](std::multimap const& l, std::span r) { if (l.size() != r.size()) return false; std::vector s(r.begin(), r.end()); std::ranges::stable_sort(s, cmp, get0); for (auto const& [vl, vr] : std::views::zip(l, s)) { if (vl != vr) return false; } return true; }; std::multimap m0(std::from_range, Range(a, a+0)); VERIFY( m0.empty() ); VERIFY( m0.get_allocator() == Alloc() ); VERIFY( is_equal(m0.key_comp(), Cmp()) ); std::multimap m4(std::from_range, Range(a, a+4), cmp); VERIFY( eq(m4, {a, 4}) ); VERIFY( m4.get_allocator() == Alloc() ); VERIFY( is_equal(m4.key_comp(), Cmp()) ); std::multimap m9(std::from_range, Range(a, a+9), alloc); VERIFY( eq(m9, {a, 9}) ); VERIFY( m9.get_allocator() == alloc ); VERIFY( is_equal(m9.key_comp(), cmp) ); std::multimap mr(std::from_range, Range(a, a+14), cmp, alloc); VERIFY( eq(mr, {a, 14}) ); VERIFY( mr.get_allocator() == alloc ); VERIFY( is_equal(mr.key_comp(), cmp) ); } template void do_test_ac() { do_test(std::allocator>(), std::less()); do_test(std::allocator>(), StateCmp{17}); do_test(__gnu_test::uneq_allocator>(42), std::less()); do_test(__gnu_test::uneq_allocator>(42), StateCmp{17}); } struct MyPair { long x; long y; constexpr operator std::pair() const { return {x, y}; } template requires (I < 2) friend constexpr long get(MyPair p) { return (I == 0) ? p.x : p.y; } constexpr friend bool operator==(MyPair lhs, std::pair rhs) { return (lhs.x == rhs.first) && (lhs.y == rhs.second); } }; bool test_ranges() { using namespace __gnu_test; do_test_ac>>(); do_test_ac, input_iterator_wrapper_nocopy>>(); do_test_ac>>(); do_test_ac>>(); do_test_ac>(); return true; } int main() { test_ranges(); }