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 9 // UNSUPPORTED: c++03, c++11, c++14 10 11 // UNSUPPORTED: availability-filesystem-missing 12 13 // <fstream> 14 15 // plate <class charT, class traits = char_traits<charT> > 16 // class basic_fstream 17 18 // template<class T> 19 // explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17 20 // Constraints: is_same_v<T, filesystem::path> is true 21 22 #include <fstream> 23 #include <filesystem> 24 #include <cassert> 25 #include <type_traits> 26 27 #include "test_macros.h" 28 #include "test_iterators.h" 29 #include "platform_support.h" 30 #include "operator_hijacker.h" 31 32 namespace fs = std::filesystem; 33 34 template <class CharT> 35 constexpr bool test_non_convert_to_path() { 36 // String types 37 static_assert(!std::is_constructible_v<std::fstream, std::basic_string_view<CharT>>); 38 static_assert(!std::is_constructible_v<std::fstream, const std::basic_string_view<CharT>>); 39 40 // Char* pointers 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::fstream, const CharT*>); 43 44 // Iterators 45 static_assert(!std::is_convertible_v<std::fstream, cpp17_input_iterator<const CharT*>>); 46 47 return true; 48 } 49 50 static_assert(test_non_convert_to_path<char>()); 51 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 55 56 #ifndef TEST_HAS_NO_CHAR8_T 57 static_assert(test_non_convert_to_path<char8_t>()); 58 #endif // TEST_HAS_NO_CHAR8_T 59 60 static_assert(test_non_convert_to_path<char16_t>()); 61 static_assert(test_non_convert_to_path<char32_t>()); 62 63 int main(int, char**) { 64 fs::path p = get_temp_file_name(); 65 { 66 std::fstream fs(p, std::ios_base::in | std::ios_base::out | 67 std::ios_base::trunc); 68 double x = 0; 69 fs << 3.25; 70 fs.seekg(0); 71 fs >> x; 72 assert(x == 3.25); 73 } 74 std::remove(p.string().c_str()); 75 76 { 77 std::basic_fstream<char, operator_hijacker_char_traits<char> > fs( 78 p, std::ios_base::in | std::ios_base::out | std::ios_base::trunc); 79 std::basic_string<char, operator_hijacker_char_traits<char> > x; 80 fs << "3.25"; 81 fs.seekg(0); 82 fs >> x; 83 assert(x == "3.25"); 84 } 85 std::remove(p.string().c_str()); 86 87 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 88 { 89 std::wfstream fs(p, std::ios_base::in | std::ios_base::out | 90 std::ios_base::trunc); 91 double x = 0; 92 fs << 3.25; 93 fs.seekg(0); 94 fs >> x; 95 assert(x == 3.25); 96 } 97 std::remove(p.string().c_str()); 98 99 { 100 std::basic_fstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs( 101 p, std::ios_base::in | std::ios_base::out | std::ios_base::trunc); 102 std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x; 103 fs << L"3.25"; 104 fs.seekg(0); 105 fs >> x; 106 assert(x == L"3.25"); 107 } 108 std::remove(p.string().c_str()); 109 110 #endif 111 112 return 0; 113 } 114