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 // FILE_DEPENDENCIES: test.dat 14 15 // <fstream> 16 17 // template <class charT, class traits = char_traits<charT> > 18 // class basic_ifstream 19 20 // template<class T> 21 // explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17 22 // Constraints: is_same_v<T, filesystem::path> is true 23 24 #include <cassert> 25 #include <filesystem> 26 #include <fstream> 27 #include <type_traits> 28 29 #include "test_macros.h" 30 #include "test_iterators.h" 31 #include "operator_hijacker.h" 32 33 namespace fs = std::filesystem; 34 35 template <class CharT> 36 constexpr bool test_non_convert_to_path() { 37 // String types 38 static_assert(!std::is_constructible_v<std::ifstream, std::basic_string_view<CharT>>); 39 static_assert(!std::is_constructible_v<std::ifstream, const std::basic_string_view<CharT>>); 40 41 // Char* pointers 42 if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>) 43 static_assert(!std::is_constructible_v<std::ifstream, const CharT*>); 44 45 // Iterators 46 static_assert(!std::is_convertible_v<std::ifstream, cpp17_input_iterator<const CharT*>>); 47 48 return true; 49 } 50 51 static_assert(test_non_convert_to_path<char>()); 52 53 #if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR) 54 static_assert(test_non_convert_to_path<wchar_t>()); 55 #endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR 56 57 #ifndef TEST_HAS_NO_CHAR8_T 58 static_assert(test_non_convert_to_path<char8_t>()); 59 #endif // TEST_HAS_NO_CHAR8_T 60 61 static_assert(test_non_convert_to_path<char16_t>()); 62 static_assert(test_non_convert_to_path<char32_t>()); 63 64 int main(int, char**) { 65 { 66 fs::path p; 67 static_assert(!std::is_convertible<fs::path, std::ifstream>::value, 68 "ctor should be explicit"); 69 static_assert(std::is_constructible<std::ifstream, fs::path const&, 70 std::ios_base::openmode>::value, 71 ""); 72 } 73 { 74 std::ifstream fs(fs::path("test.dat")); 75 double x = 0; 76 fs >> x; 77 assert(x == 3.25); 78 } 79 { 80 std::basic_ifstream<char, operator_hijacker_char_traits<char>> fs(fs::path("test.dat")); 81 std::basic_string<char, operator_hijacker_char_traits<char> > x; 82 fs >> x; 83 assert(x == "3.25"); 84 } 85 // std::ifstream(const fs::path&, std::ios_base::openmode) is tested in 86 // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp 87 // which creates writable files. 88 89 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 90 { 91 std::wifstream fs(fs::path("test.dat")); 92 double x = 0; 93 fs >> x; 94 assert(x == 3.25); 95 } 96 { 97 std::basic_ifstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(fs::path("test.dat")); 98 std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x; 99 fs >> x; 100 assert(x == L"3.25"); 101 } 102 // std::wifstream(const fs::path&, std::ios_base::openmode) is tested in 103 // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp 104 // which creates writable files. 105 #endif 106 107 return 0; 108 } 109