| 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 | // <iterator> |
| 10 | |
| 11 | // class istream_iterator |
| 12 | |
| 13 | // istream_iterator(const istream_iterator& x) noexcept(see below); |
| 14 | |
| 15 | #include <iterator> |
| 16 | #include <sstream> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | |
| 21 | // The copy constructor is constexpr in C++11, but that is not easy to test. |
| 22 | // The comparison of the class is not constexpr so this is only a compile test. |
| 23 | TEST_CONSTEXPR_CXX14 bool test_constexpr() { |
| 24 | std::istream_iterator<int> io; |
| 25 | [[maybe_unused]] std::istream_iterator<int> i = io; |
| 26 | |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | struct thowing_copy_constructor { |
| 31 | thowing_copy_constructor() {} |
| 32 | thowing_copy_constructor(const thowing_copy_constructor&) TEST_NOEXCEPT_FALSE {} |
| 33 | }; |
| 34 | |
| 35 | int main(int, char**) |
| 36 | { |
| 37 | { |
| 38 | std::istream_iterator<int> io; |
| 39 | std::istream_iterator<int> i = io; |
| 40 | assert(i == std::istream_iterator<int>()); |
| 41 | #if TEST_STD_VER >= 11 |
| 42 | static_assert(std::is_nothrow_copy_constructible<std::istream_iterator<int>>::value, "" ); |
| 43 | #endif |
| 44 | } |
| 45 | { |
| 46 | std::istream_iterator<thowing_copy_constructor> io; |
| 47 | std::istream_iterator<thowing_copy_constructor> i = io; |
| 48 | assert(i == std::istream_iterator<thowing_copy_constructor>()); |
| 49 | #if TEST_STD_VER >= 11 |
| 50 | static_assert(!std::is_nothrow_copy_constructible<std::istream_iterator<thowing_copy_constructor>>::value, "" ); |
| 51 | #endif |
| 52 | } |
| 53 | { |
| 54 | std::istringstream inf(" 1 23" ); |
| 55 | std::istream_iterator<int> io(inf); |
| 56 | std::istream_iterator<int> i = io; |
| 57 | assert(i != std::istream_iterator<int>()); |
| 58 | int j = 0; |
| 59 | j = *i; |
| 60 | assert(j == 1); |
| 61 | } |
| 62 | |
| 63 | #if TEST_STD_VER >= 14 |
| 64 | static_assert(test_constexpr(), "" ); |
| 65 | #endif |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |