131 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <string>10// UNSUPPORTED: c++03, c++11, c++1411 12// template<class InputIterator,13// class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>14// basic_string(InputIterator, InputIterator, Allocator = Allocator())15// -> basic_string<typename iterator_traits<InputIterator>::value_type,16// char_traits<typename iterator_traits<InputIterator>::value_type>,17// Allocator>; // constexpr since C++2018//19// The deduction guide shall not participate in overload resolution if InputIterator20// is a type that does not qualify as an input iterator, or if Allocator is a type21// that does not qualify as an allocator.22 23#include <cassert>24#include <cstddef>25#include <iterator>26#include <string>27#include <type_traits>28#include <utility>29 30#include "test_macros.h"31#include "test_allocator.h"32#include "min_allocator.h"33#include "asan_testing.h"34 35class NotAnIterator {};36using NotAnInputIterator = std::back_insert_iterator<std::basic_string<char16_t>>;37 38template <typename T>39struct NotAnAllocator {40 typedef T value_type;41};42 43template <class Iter, class Alloc, class = void>44struct CanDeduce : std::false_type {};45 46template <class Iter, class Alloc>47struct CanDeduce<Iter,48 Alloc,49 decltype((void)std::basic_string{std::declval<Iter>(), std::declval<Iter>(), std::declval<Alloc>()})>50 : std::true_type {};51 52static_assert(CanDeduce<char*, std::allocator<char>>::value);53static_assert(!CanDeduce<NotAnIterator, std::allocator<char>>::value);54static_assert(!CanDeduce<NotAnInputIterator, std::allocator<char16_t>>::value);55static_assert(!CanDeduce<char*, NotAnAllocator<char>>::value);56#ifndef TEST_HAS_NO_WIDE_CHARACTERS57static_assert(CanDeduce<wchar_t*, std::allocator<wchar_t>>::value);58static_assert(!CanDeduce<wchar_t const*, NotAnAllocator<wchar_t>>::value);59#endif60 61TEST_CONSTEXPR_CXX20 bool test() {62 {63 const char* s = "12345678901234";64 std::basic_string s1(s, s + 10); // Can't use {} here65 using S = decltype(s1); // what type did we get?66 static_assert(std::is_same_v<S::value_type, char>, "");67 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");68 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");69 assert(s1.size() == 10);70 assert(s1.compare(0, s1.size(), s, s1.size()) == 0);71 LIBCPP_ASSERT(is_string_asan_correct(s1));72 }73 {74 const char* s = "12345678901234";75 std::basic_string s1{s, s + 10, std::allocator<char>{}};76 using S = decltype(s1); // what type did we get?77 static_assert(std::is_same_v<S::value_type, char>, "");78 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");79 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");80 assert(s1.size() == 10);81 assert(s1.compare(0, s1.size(), s, s1.size()) == 0);82 LIBCPP_ASSERT(is_string_asan_correct(s1));83 }84#ifndef TEST_HAS_NO_WIDE_CHARACTERS85 {86 const wchar_t* s = L"12345678901234";87 std::basic_string s1{s, s + 10, test_allocator<wchar_t>{}};88 using S = decltype(s1); // what type did we get?89 static_assert(std::is_same_v<S::value_type, wchar_t>, "");90 static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");91 static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");92 assert(s1.size() == 10);93 assert(s1.compare(0, s1.size(), s, s1.size()) == 0);94 LIBCPP_ASSERT(is_string_asan_correct(s1));95 }96#endif97 {98 const char16_t* s = u"12345678901234";99 std::basic_string s1{s, s + 10, min_allocator<char16_t>{}};100 using S = decltype(s1); // what type did we get?101 static_assert(std::is_same_v<S::value_type, char16_t>, "");102 static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");103 static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");104 assert(s1.size() == 10);105 assert(s1.compare(0, s1.size(), s, s1.size()) == 0);106 LIBCPP_ASSERT(is_string_asan_correct(s1));107 }108 {109 const char32_t* s = U"12345678901234";110 std::basic_string s1{s, s + 10, explicit_allocator<char32_t>{}};111 using S = decltype(s1); // what type did we get?112 static_assert(std::is_same_v<S::value_type, char32_t>, "");113 static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");114 static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");115 assert(s1.size() == 10);116 assert(s1.compare(0, s1.size(), s, s1.size()) == 0);117 LIBCPP_ASSERT(is_string_asan_correct(s1));118 }119 120 return true;121}122 123int main(int, char**) {124 test();125#if TEST_STD_VER > 17126 static_assert(test());127#endif128 129 return 0;130}131