xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/from_range_deduction.pass.cpp (revision 64addd65210db59c8cb3794f792720e03e25b5af)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <string>
10 
11 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
12 // To silence a GCC warning-turned-error re. `BadAlloc::value_type`.
13 // ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-unused-local-typedefs
14 
15 // template<ranges::input_range R,
16 //          class Allocator = allocator<ranges::range_value_t<R>>>
17 //   basic_string(from_range_t, R&&, Allocator = Allocator())
18 //     -> basic_string<ranges::range_value_t<R>, char_traits<ranges::range_value_t<R>>,
19 //                     Allocator>; // C++23
20 //
21 // The deduction guide shall not participate in overload resolution if Allocator
22 // is a type that does not qualify as an allocator (in addition to the `input_range` concept being satisfied by `R`).
23 
24 #include <array>
25 #include <string>
26 
27 #include "deduction_guides_sfinae_checks.h"
28 #include "test_allocator.h"
29 #include "asan_testing.h"
30 
main(int,char **)31 int main(int, char**) {
32   using Char = char16_t;
33 
34   {
35     std::basic_string c(std::from_range, std::array<Char, 0>());
36     static_assert(std::is_same_v<decltype(c), std::basic_string<Char>>);
37     LIBCPP_ASSERT(is_string_asan_correct(c));
38   }
39 
40   {
41     using Alloc = test_allocator<Char>;
42     std::basic_string c(std::from_range, std::array<Char, 0>(), Alloc());
43     static_assert(std::is_same_v<decltype(c), std::basic_string<Char, std::char_traits<Char>, Alloc>>);
44     LIBCPP_ASSERT(is_string_asan_correct(c));
45   }
46 
47   // Note: defining `value_type` is a workaround because one of the deduction guides will end up instantiating
48   // `basic_string`, and that would fail with a hard error if the given allocator doesn't define `value_type`.
49   struct BadAlloc {
50     using value_type = char;
51   };
52   SequenceContainerDeductionGuidesSfinaeAway<std::basic_string, std::basic_string<char>, BadAlloc>();
53 
54   return 0;
55 }
56