1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
13 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT>>
14 // class basic_ostringstream
17 // explicit basic_ostringstream(const T& t, ios_base::openmode which);
24 #include <string_view>
26 #include "constexpr_char_traits.h"
27 #include "nasty_string.h"
28 #include "test_allocator.h"
29 #include "test_convertible.h"
30 #include "test_macros.h"
32 #include "../../helper_string_macros.h"
33 #include "../../helper_types.h"
35 template <typename AllocT
= std::allocator
<nasty_char
>>
36 void test_sfinae_with_nasty_char() {
38 using NStrStream
= std::basic_ostringstream
<nasty_char
, nasty_char_traits
, test_allocator
<nasty_char
>>;
40 static_assert(std::constructible_from
<NStrStream
, nasty_char
*, test_allocator
<nasty_char
>>);
41 static_assert(!test_convertible
<NStrStream
, nasty_char
*, std::ios_base::openmode
>());
44 using NStrStream
= std::basic_ostringstream
<nasty_char
, nasty_char_traits
, test_allocator
<nasty_char
>>;
46 static_assert(std::constructible_from
<NStrStream
, const nasty_char
*, test_allocator
<nasty_char
>>);
47 static_assert(!test_convertible
<NStrStream
, const nasty_char
*, std::ios_base::openmode
>());
50 template <typename CharT
, typename TraitsT
= std::char_traits
<CharT
>, typename AllocT
= std::allocator
<CharT
>>
52 using StrStream
= std::basic_ostringstream
<CharT
, TraitsT
, AllocT
>;
55 static_assert(std::constructible_from
<StrStream
, CharT
*, std::ios_base::openmode
>);
56 static_assert(!test_convertible
<StrStream
, CharT
*, std::ios_base::openmode
>());
59 static_assert(std::constructible_from
<StrStream
, const CharT
*, std::ios_base::openmode
>);
60 static_assert(!test_convertible
<StrStream
, const CharT
*, std::ios_base::openmode
>());
62 // `std::basic_string_view<CharT>`
64 std::constructible_from
<StrStream
, const std::basic_string_view
<CharT
, TraitsT
>, std::ios_base::openmode
>);
65 static_assert(!test_convertible
<StrStream
, const std::basic_string_view
<CharT
, TraitsT
>, std::ios_base::openmode
>());
67 // `std::basic_string<CharT>`
68 static_assert(std::constructible_from
<StrStream
, const std::basic_string
<CharT
, TraitsT
>, std::ios_base::openmode
>);
69 static_assert(!test_convertible
<StrStream
, const std::basic_string
<CharT
, TraitsT
>, std::ios_base::openmode
>());
71 // ConstConvertibleStringView<CharT>
73 std::constructible_from
<StrStream
, const ConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>);
75 !test_convertible
<StrStream
, const ConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>());
77 // NonConstConvertibleStringView<CharT>
79 !std::constructible_from
<StrStream
, NonConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>);
80 static_assert(!test_convertible
<StrStream
, NonConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>());
84 constructible_from
<StrStream
, const NonConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>);
86 !test_convertible
<StrStream
, const NonConstConvertibleStringView
<CharT
, TraitsT
>, std::ios_base::openmode
>());
88 // Non-`string-view-like`
89 static_assert(!std::constructible_from
<StrStream
, const SomeObject
, std::ios_base::openmode
>);
90 static_assert(!test_convertible
<StrStream
, const SomeObject
, std::ios_base::openmode
>());
93 static_assert(!std::constructible_from
<StrStream
, const std::basic_string_view
<CharT
, TraitsT
>, const SomeObject
>);
94 static_assert(!test_convertible
<StrStream
, const std::basic_string_view
<CharT
, TraitsT
>, const SomeObject
>());
97 template <typename CharT
, typename TraitsT
= std::char_traits
<CharT
>, typename AllocT
= std::allocator
<CharT
>>
99 using StrStream
= std::basic_ostringstream
<CharT
, TraitsT
, AllocT
>;
101 const AllocT allocator
;
105 StrStream
ss(CS("zmt"), std::ios_base::binary
);
106 assert(ss
.str() == CS("zmt"));
108 // std::basic_string_view<CharT>
110 const std::basic_string_view
<CharT
, TraitsT
> csv
= SV("zmt");
111 StrStream
ss(csv
, std::ios_base::binary
);
112 assert(ss
.str() == CS("zmt"));
114 // std::basic_string<CharT>
116 const std::basic_string
<CharT
, TraitsT
, AllocT
> cs
= ST("zmt", allocator
);
117 StrStream
ss(cs
, std::ios_base::binary
);
118 assert(ss
.str() == CS("zmt"));
120 // ConstConvertibleStringView<CharT>
122 const ConstConvertibleStringView
<CharT
, TraitsT
> sv
{CS("zmt")};
123 StrStream
ss(sv
, std::ios_base::binary
);
124 assert(ss
.str() == CS("zmt"));
128 int main(int, char**) {
129 test_sfinae_with_nasty_char();
130 test_sfinae_with_nasty_char
<test_allocator
<nasty_char
>>();
132 test_sfinae
<char, constexpr_char_traits
<char>, std::allocator
<char>>();
133 test_sfinae
<char, std::char_traits
<char>, test_allocator
<char>>();
134 test_sfinae
<char, constexpr_char_traits
<char>, test_allocator
<char>>();
136 test
<char, constexpr_char_traits
<char>, std::allocator
<char>>();
137 test
<char, std::char_traits
<char>, test_allocator
<char>>();
138 test
<char, constexpr_char_traits
<char>, test_allocator
<char>>();
139 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
140 test_sfinae
<wchar_t>();
141 test_sfinae
<wchar_t, constexpr_char_traits
<wchar_t>, std::allocator
<wchar_t>>();
142 test_sfinae
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>();
143 test_sfinae
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>();
145 test
<wchar_t, constexpr_char_traits
<wchar_t>, std::allocator
<wchar_t>>();
146 test
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>();
147 test
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>();