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
11 // UNSUPPORTED: availability-filesystem-missing
15 // plate <class charT, class traits = char_traits<charT> >
16 // class basic_ofstream
19 // explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
20 // Constraints: is_same_v<T, filesystem::path> is true
25 #include <type_traits>
27 #include "platform_support.h"
28 #include "operator_hijacker.h"
29 #include "test_macros.h"
30 #include "test_iterators.h"
32 namespace fs
= std::filesystem
;
34 template <class CharT
>
35 constexpr bool test_non_convert_to_path() {
37 static_assert(!std::is_constructible_v
<std::ofstream
, std::basic_string_view
<CharT
>>);
38 static_assert(!std::is_constructible_v
<std::ofstream
, const std::basic_string_view
<CharT
>>);
41 if constexpr (!std::is_same_v
<CharT
, char> && !std::is_same_v
<CharT
, fs::path::value_type
>)
42 static_assert(!std::is_constructible_v
<std::ofstream
, const CharT
*>);
45 static_assert(!std::is_convertible_v
<std::ofstream
, cpp17_input_iterator
<const CharT
*>>);
50 static_assert(test_non_convert_to_path
<char>());
52 #if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR)
53 static_assert(test_non_convert_to_path
<wchar_t>());
54 #endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR
56 #ifndef TEST_HAS_NO_CHAR8_T
57 static_assert(test_non_convert_to_path
<char8_t
>());
58 #endif // TEST_HAS_NO_CHAR8_T
60 static_assert(test_non_convert_to_path
<char16_t
>());
61 static_assert(test_non_convert_to_path
<char32_t
>());
63 int main(int, char**) {
65 static_assert(!std::is_convertible
<fs::path
, std::ofstream
>::value
,
66 "ctor should be explicit");
67 static_assert(std::is_constructible
<std::ofstream
, fs::path
const&,
68 std::ios_base::openmode
>::value
,
71 fs::path p
= get_temp_file_name();
73 std::ofstream
stream(p
);
77 std::ifstream
stream(p
);
82 std::remove(p
.string().c_str());
85 std::basic_ofstream
<char, operator_hijacker_char_traits
<char> > stream(p
);
89 std::ifstream
stream(p
);
94 std::remove(p
.string().c_str());
97 std::ofstream
stream(p
, std::ios_base::out
);
101 std::ifstream
stream(p
);
106 std::remove(p
.string().c_str());
109 std::basic_ofstream
<char, operator_hijacker_char_traits
<char> > stream(p
, std::ios_base::out
);
113 std::ifstream
stream(p
);
118 std::remove(p
.string().c_str());
120 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
122 std::wofstream
stream(p
);
126 std::wifstream
stream(p
);
131 std::remove(p
.string().c_str());
134 std::basic_ofstream
<wchar_t, operator_hijacker_char_traits
<wchar_t> > stream(p
);
138 std::wifstream
stream(p
);
143 std::remove(p
.string().c_str());
146 std::wofstream
stream(p
, std::ios_base::out
);
150 std::wifstream
stream(p
);
155 std::remove(p
.string().c_str());
158 std::basic_ofstream
<wchar_t, operator_hijacker_char_traits
<wchar_t> > stream(p
, std::ios_base::out
);
162 std::wifstream
stream(p
);
167 std::remove(p
.string().c_str());