brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · b1ff8ff Raw
107 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// FILE_DEPENDENCIES: test.dat12 13// <fstream>14 15// template <class charT, class traits = char_traits<charT> >16// class basic_ifstream17 18// template<class T>19// explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++1720// Constraints: is_same_v<T, filesystem::path> is true21 22#include <cassert>23#include <filesystem>24#include <fstream>25#include <type_traits>26 27#include "test_macros.h"28#include "test_iterators.h"29#include "operator_hijacker.h"30 31namespace fs = std::filesystem;32 33template <class CharT>34constexpr bool test_non_convert_to_path() {35  // String types36  static_assert(!std::is_constructible_v<std::ifstream, std::basic_string_view<CharT>>);37  static_assert(!std::is_constructible_v<std::ifstream, const std::basic_string_view<CharT>>);38 39  // Char* pointers40  if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)41    static_assert(!std::is_constructible_v<std::ifstream, const CharT*>);42 43  // Iterators44  static_assert(!std::is_convertible_v<std::ifstream, cpp17_input_iterator<const CharT*>>);45 46  return true;47}48 49static_assert(test_non_convert_to_path<char>());50 51#if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR)52static_assert(test_non_convert_to_path<wchar_t>());53#endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR54 55#ifndef TEST_HAS_NO_CHAR8_T56static_assert(test_non_convert_to_path<char8_t>());57#endif // TEST_HAS_NO_CHAR8_T58 59static_assert(test_non_convert_to_path<char16_t>());60static_assert(test_non_convert_to_path<char32_t>());61 62int main(int, char**) {63  {64    fs::path p;65    static_assert(!std::is_convertible<fs::path, std::ifstream>::value,66                  "ctor should be explicit");67    static_assert(std::is_constructible<std::ifstream, fs::path const&,68                                        std::ios_base::openmode>::value,69                  "");70  }71  {72    std::ifstream fs(fs::path("test.dat"));73    double x = 0;74    fs >> x;75    assert(x == 3.25);76  }77  {78    std::basic_ifstream<char, operator_hijacker_char_traits<char>> fs(fs::path("test.dat"));79    std::basic_string<char, operator_hijacker_char_traits<char> > x;80    fs >> x;81    assert(x == "3.25");82  }83  // std::ifstream(const fs::path&, std::ios_base::openmode) is tested in84  // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp85  // which creates writable files.86 87#ifndef TEST_HAS_NO_WIDE_CHARACTERS88  {89    std::wifstream fs(fs::path("test.dat"));90    double x = 0;91    fs >> x;92    assert(x == 3.25);93  }94  {95    std::basic_ifstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(fs::path("test.dat"));96    std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x;97    fs >> x;98    assert(x == L"3.25");99  }100  // std::wifstream(const fs::path&, std::ios_base::openmode) is tested in101  // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp102  // which creates writable files.103#endif104 105  return 0;106}107