126 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 charT,13// class traits,14// class Allocator = allocator<charT>15// >16// basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())17// -> basic_string<charT, traits, Allocator>;18//19// The deduction guide shall not participate in overload resolution if Allocator20// is a type that does not qualify as an allocator.21 22#include <cassert>23#include <cstddef>24#include <iterator>25#include <memory>26#include <string>27#include <string_view>28#include <type_traits>29#include <utility>30 31#include "min_allocator.h"32#include "test_allocator.h"33#include "test_macros.h"34 35template <class StringView, class Allocator, class = void>36struct CanDeduce : std::false_type {};37 38template <class StringView, class Allocator>39struct CanDeduce<StringView,40 Allocator,41 decltype((void)std::basic_string{std::declval<StringView>(), std::declval<Allocator>()})>42 : std::true_type {};43 44struct NotAnAllocator {};45static_assert(CanDeduce<std::string_view, std::allocator<char>>::value);46static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value);47 48TEST_CONSTEXPR_CXX20 bool test() {49 {50 std::string_view sv = "12345678901234";51 std::basic_string s1(sv);52 using S = decltype(s1); // what type did we get?53 static_assert(std::is_same_v<S::value_type, char>, "");54 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");55 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");56 assert(s1.size() == sv.size());57 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);58 }59 60 {61 std::string_view sv = "12345678901234";62 std::basic_string s1{sv, std::allocator<char>{}};63 using S = decltype(s1); // what type did we get?64 static_assert(std::is_same_v<S::value_type, char>, "");65 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");66 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");67 assert(s1.size() == sv.size());68 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);69 }70#ifndef TEST_HAS_NO_WIDE_CHARACTERS71 {72 std::wstring_view sv = L"12345678901234";73 std::basic_string s1{sv, test_allocator<wchar_t>{}};74 using S = decltype(s1); // what type did we get?75 static_assert(std::is_same_v<S::value_type, wchar_t>, "");76 static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");77 static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");78 assert(s1.size() == sv.size());79 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);80 }81#endif82#ifndef TEST_HAS_NO_CHAR8_T83 {84 std::u8string_view sv = u8"12345678901234";85 std::basic_string s1{sv, min_allocator<char8_t>{}};86 using S = decltype(s1); // what type did we get?87 static_assert(std::is_same_v<S::value_type, char8_t>, "");88 static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");89 static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");90 assert(s1.size() == sv.size());91 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);92 }93#endif94 {95 std::u16string_view sv = u"12345678901234";96 std::basic_string s1{sv, min_allocator<char16_t>{}};97 using S = decltype(s1); // what type did we get?98 static_assert(std::is_same_v<S::value_type, char16_t>, "");99 static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");100 static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");101 assert(s1.size() == sv.size());102 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);103 }104 {105 std::u32string_view sv = U"12345678901234";106 std::basic_string s1{sv, explicit_allocator<char32_t>{}};107 using S = decltype(s1); // what type did we get?108 static_assert(std::is_same_v<S::value_type, char32_t>, "");109 static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");110 static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");111 assert(s1.size() == sv.size());112 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);113 }114 115 return true;116}117 118int main(int, char**) {119 test();120#if TEST_STD_VER > 17121 static_assert(test());122#endif123 124 return 0;125}126