string_view_deduction.pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // <string>
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: libcpp-no-deduction-guides
  11. // template<class InputIterator>
  12. // basic_string(InputIterator begin, InputIterator end,
  13. // const Allocator& a = Allocator());
  14. // template<class charT,
  15. // class traits,
  16. // class Allocator = allocator<charT>
  17. // >
  18. // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
  19. // -> basic_string<charT, traits, Allocator>;
  20. //
  21. // The deduction guide shall not participate in overload resolution if Allocator
  22. // is a type that does not qualify as an allocator.
  23. #include <string>
  24. #include <string_view>
  25. #include <iterator>
  26. #include <memory>
  27. #include <type_traits>
  28. #include <cassert>
  29. #include <cstddef>
  30. #include "test_macros.h"
  31. #include "test_allocator.h"
  32. #include "../input_iterator.h"
  33. #include "min_allocator.h"
  34. int main(int, char**)
  35. {
  36. {
  37. std::string_view sv = "12345678901234";
  38. std::basic_string s1(sv);
  39. using S = decltype(s1); // what type did we get?
  40. static_assert(std::is_same_v<S::value_type, char>, "");
  41. static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
  42. static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
  43. assert(s1.size() == sv.size());
  44. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  45. }
  46. {
  47. std::string_view sv = "12345678901234";
  48. std::basic_string s1{sv, std::allocator<char>{}};
  49. using S = decltype(s1); // what type did we get?
  50. static_assert(std::is_same_v<S::value_type, char>, "");
  51. static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
  52. static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
  53. assert(s1.size() == sv.size());
  54. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  55. }
  56. {
  57. std::wstring_view sv = L"12345678901234";
  58. std::basic_string s1{sv, test_allocator<wchar_t>{}};
  59. using S = decltype(s1); // what type did we get?
  60. static_assert(std::is_same_v<S::value_type, wchar_t>, "");
  61. static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
  62. static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
  63. assert(s1.size() == sv.size());
  64. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  65. }
  66. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  67. {
  68. std::u8string_view sv = u8"12345678901234";
  69. std::basic_string s1{sv, min_allocator<char8_t>{}};
  70. using S = decltype(s1); // what type did we get?
  71. static_assert(std::is_same_v<S::value_type, char8_t>, "");
  72. static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");
  73. static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");
  74. assert(s1.size() == sv.size());
  75. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  76. }
  77. #endif
  78. {
  79. std::u16string_view sv = u"12345678901234";
  80. std::basic_string s1{sv, min_allocator<char16_t>{}};
  81. using S = decltype(s1); // what type did we get?
  82. static_assert(std::is_same_v<S::value_type, char16_t>, "");
  83. static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
  84. static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
  85. assert(s1.size() == sv.size());
  86. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  87. }
  88. {
  89. std::u32string_view sv = U"12345678901234";
  90. std::basic_string s1{sv, explicit_allocator<char32_t>{}};
  91. using S = decltype(s1); // what type did we get?
  92. static_assert(std::is_same_v<S::value_type, char32_t>, "");
  93. static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
  94. static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
  95. assert(s1.size() == sv.size());
  96. assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
  97. }
  98. return 0;
  99. }