xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
176b26852SMarshall Clow //===----------------------------------------------------------------------===//
276b26852SMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
676b26852SMarshall Clow //
776b26852SMarshall Clow //===----------------------------------------------------------------------===//
876b26852SMarshall Clow 
976b26852SMarshall Clow // <string>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
1176b26852SMarshall Clow 
1276b26852SMarshall Clow // template<class charT,
1376b26852SMarshall Clow //          class traits,
1476b26852SMarshall Clow //          class Allocator = allocator<charT>
1576b26852SMarshall Clow //          >
1676b26852SMarshall Clow // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
1776b26852SMarshall Clow //   -> basic_string<charT, traits, Allocator>;
1876b26852SMarshall Clow //
1976b26852SMarshall Clow //  The deduction guide shall not participate in overload resolution if Allocator
2076b26852SMarshall Clow //  is a type that does not qualify as an allocator.
2176b26852SMarshall Clow 
2276b26852SMarshall Clow #include <string>
2376b26852SMarshall Clow #include <string_view>
2476b26852SMarshall Clow #include <iterator>
2576b26852SMarshall Clow #include <memory>
2676b26852SMarshall Clow #include <type_traits>
2776b26852SMarshall Clow #include <cassert>
2876b26852SMarshall Clow #include <cstddef>
2976b26852SMarshall Clow 
3076b26852SMarshall Clow #include "test_macros.h"
3176b26852SMarshall Clow #include "test_allocator.h"
3276b26852SMarshall Clow #include "min_allocator.h"
3376b26852SMarshall Clow 
348ec49997SLouis Dionne template <class StringView, class Allocator, class = void>
358ec49997SLouis Dionne struct CanDeduce : std::false_type {};
368ec49997SLouis Dionne 
378ec49997SLouis Dionne template <class StringView, class Allocator>
38*a40bada9SBrendan Emery struct CanDeduce<StringView,
39*a40bada9SBrendan Emery                  Allocator,
40*a40bada9SBrendan Emery                  decltype((void)std::basic_string{std::declval<StringView>(), std::declval<Allocator>()})>
41*a40bada9SBrendan Emery     : std::true_type {};
428ec49997SLouis Dionne 
438ec49997SLouis Dionne struct NotAnAllocator {};
448ec49997SLouis Dionne static_assert(CanDeduce<std::string_view, std::allocator<char>>::value);
458ec49997SLouis Dionne static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value);
468ec49997SLouis Dionne 
test()47425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
4876b26852SMarshall Clow   {
4976b26852SMarshall Clow     std::string_view sv = "12345678901234";
5076b26852SMarshall Clow     std::basic_string s1(sv);
5176b26852SMarshall Clow     using S = decltype(s1); // what type did we get?
5276b26852SMarshall Clow     static_assert(std::is_same_v<S::value_type, char>, "");
5376b26852SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
5476b26852SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
5576b26852SMarshall Clow     assert(s1.size() == sv.size());
5676b26852SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
5776b26852SMarshall Clow   }
5876b26852SMarshall Clow 
5976b26852SMarshall Clow   {
6076b26852SMarshall Clow     std::string_view sv = "12345678901234";
6176b26852SMarshall Clow     std::basic_string s1{sv, std::allocator<char>{}};
6276b26852SMarshall Clow     using S = decltype(s1); // what type did we get?
6376b26852SMarshall Clow     static_assert(std::is_same_v<S::value_type, char>, "");
6476b26852SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
6576b26852SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
6676b26852SMarshall Clow     assert(s1.size() == sv.size());
6776b26852SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
6876b26852SMarshall Clow   }
69f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
7076b26852SMarshall Clow   {
7176b26852SMarshall Clow     std::wstring_view sv = L"12345678901234";
7276b26852SMarshall Clow     std::basic_string s1{sv, test_allocator<wchar_t>{}};
7376b26852SMarshall Clow     using S = decltype(s1); // what type did we get?
7476b26852SMarshall Clow     static_assert(std::is_same_v<S::value_type, wchar_t>, "");
7576b26852SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
7676b26852SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
7776b26852SMarshall Clow     assert(s1.size() == sv.size());
7876b26852SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
7976b26852SMarshall Clow   }
80f4c1258dSLouis Dionne #endif
81425620ccSNikolas Klauser #ifndef TEST_HAS_NO_CHAR8_T
827dad0bd6SMarshall Clow   {
837dad0bd6SMarshall Clow     std::u8string_view sv = u8"12345678901234";
847dad0bd6SMarshall Clow     std::basic_string s1{sv, min_allocator<char8_t>{}};
857dad0bd6SMarshall Clow     using S = decltype(s1); // what type did we get?
867dad0bd6SMarshall Clow     static_assert(std::is_same_v<S::value_type, char8_t>, "");
877dad0bd6SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");
887dad0bd6SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");
897dad0bd6SMarshall Clow     assert(s1.size() == sv.size());
907dad0bd6SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
917dad0bd6SMarshall Clow   }
927dad0bd6SMarshall Clow #endif
9376b26852SMarshall Clow   {
9476b26852SMarshall Clow     std::u16string_view sv = u"12345678901234";
9576b26852SMarshall Clow     std::basic_string s1{sv, min_allocator<char16_t>{}};
9676b26852SMarshall Clow     using S = decltype(s1); // what type did we get?
9776b26852SMarshall Clow     static_assert(std::is_same_v<S::value_type, char16_t>, "");
9876b26852SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
9976b26852SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
10076b26852SMarshall Clow     assert(s1.size() == sv.size());
10176b26852SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
10276b26852SMarshall Clow   }
10376b26852SMarshall Clow   {
10476b26852SMarshall Clow     std::u32string_view sv = U"12345678901234";
10576b26852SMarshall Clow     std::basic_string s1{sv, explicit_allocator<char32_t>{}};
10676b26852SMarshall Clow     using S = decltype(s1); // what type did we get?
10776b26852SMarshall Clow     static_assert(std::is_same_v<S::value_type, char32_t>, "");
10876b26852SMarshall Clow     static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
10976b26852SMarshall Clow     static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
11076b26852SMarshall Clow     assert(s1.size() == sv.size());
11176b26852SMarshall Clow     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
11276b26852SMarshall Clow   }
1132df59c50SJF Bastien 
114e85018b7SNikolas Klauser   return true;
115e85018b7SNikolas Klauser }
116e85018b7SNikolas Klauser 
main(int,char **)117*a40bada9SBrendan Emery int main(int, char**) {
118e85018b7SNikolas Klauser   test();
119e85018b7SNikolas Klauser #if TEST_STD_VER > 17
120425620ccSNikolas Klauser   static_assert(test());
121e85018b7SNikolas Klauser #endif
122e85018b7SNikolas Klauser 
1232df59c50SJF Bastien   return 0;
12476b26852SMarshall Clow }
125