// { 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 void test_deduction_guide(long* p) { __gnu_test::test_input_range r(p, p); std::vector v(std::from_range, r); static_assert(std::is_same_v>); using Alloc = __gnu_test::SimpleAllocator; Alloc alloc; std::vector v2(std::from_range, r, alloc); static_assert(std::is_same_v>); } template constexpr void do_test(Alloc alloc) { // The vector's value_type. using V = typename std::allocator_traits::value_type; // The range's value_type. using T = std::ranges::range_value_t; T a[]{1,2,3,4,5,6,7,8,9}; auto eq = [](const std::vector& l, std::span r) { if (l.size() != r.size()) return false; for (auto i = 0u; i < l.size(); ++i) if (l[i] != r[i]) return false; return true; }; std::vector v0(std::from_range, Range(a, a+0)); VERIFY( v0.empty() ); VERIFY( v0.get_allocator() == Alloc() ); std::vector v4(std::from_range, Range(a, a+4)); VERIFY( eq(v4, {a, 4}) ); VERIFY( v4.get_allocator() == Alloc() ); std::vector v9(std::from_range, Range(a, a+9), alloc); VERIFY( eq(v9, {a, 9}) ); VERIFY( v9.get_allocator() == alloc ); } template void do_test_a() { do_test(std::allocator()); do_test(__gnu_test::uneq_allocator(42)); } bool test_ranges() { using namespace __gnu_test; do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); do_test_a>(); // Not lvalue-convertible to int struct C { C(int v) : val(v) { } operator int() && { return val; } bool operator==(int b) const { return b == val; } int val; }; using rvalue_input_range = test_range; do_test(std::allocator()); return true; } constexpr bool test_constexpr() { // XXX: this doesn't test the non-forward_range code paths are constexpr. do_test>(std::allocator()); return true; } void test_pr120367() { #ifdef __cpp_exceptions struct X { X(int) { throw 1; } // Cannot successfully construct an X. ~X() { VERIFY(false); } // So should never need to destroy one. }; try { int i[1]{}; std::vector v(std::from_range, i); } catch (int) { } #endif } int main() { test_ranges(); static_assert( test_constexpr() ); test_pr120367(); }