Skip to content

Commit ccd06e4

Browse files
authored
[libc++][istream] P3223R2: Making std::istream::ignore less surprising (#147007)
Implements https://wg21.link/P3223R2 as a DR as, as recommended in cplusplus/papers#1871 (comment). Resolves -1L ambiguity. Closes #148178
1 parent e8e0b3d commit ccd06e4

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Implemented Papers
4141
- P2321R2: ``zip`` (`Github <https://llvm.org/PR105169>`__) (The paper is partially implemented. ``zip_transform_view``
4242
is implemented in this release)
4343
- P3044R2: sub-``string_view`` from ``string`` (`Github <https://llvm.org/PR148140>`__)
44+
- P3223R2: Making ``std::istream::ignore`` less surprising (`Github <https://llvm.org/PR148178>`__)
4445
- P3168R2: Give ``std::optional`` Range Support (`Github <https://llvm.org/PR105430>`__)
4546

4647
Improvements and New Features

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
"`P3111R8 <https://wg21.link/P3111R8>`__","Atomic Reduction Operations","2025-06 (Sofia)","","","`#148174 <https://github.com/llvm/llvm-project/issues/148174>`__",""
152152
"`P3060R3 <https://wg21.link/P3060R3>`__","Add ``std::views::indices(n)``","2025-06 (Sofia)","","","`#148175 <https://github.com/llvm/llvm-project/issues/148175>`__",""
153153
"`P2319R5 <https://wg21.link/P2319R5>`__","Prevent ``path`` presentation problems","2025-06 (Sofia)","","","`#148177 <https://github.com/llvm/llvm-project/issues/148177>`__",""
154-
"`P3223R2 <https://wg21.link/P3223R2>`__","Making ``std::istream::ignore`` less surprising","2025-06 (Sofia)","","","`#148178 <https://github.com/llvm/llvm-project/issues/148178>`__",""
154+
"`P3223R2 <https://wg21.link/P3223R2>`__","Making ``std::istream::ignore`` less surprising","2025-06 (Sofia)","|Complete|","22","`#148178 <https://github.com/llvm/llvm-project/issues/148178>`__",""
155155
"`P2781R9 <https://wg21.link/P2781R9>`__","``std::constant_wrapper``","2025-06 (Sofia)","","","`#148179 <https://github.com/llvm/llvm-project/issues/148179>`__",""
156156
"`P3697R1 <https://wg21.link/P3697R1>`__","Minor additions to C++26 standard library hardening","2025-06 (Sofia)","","","`#148180 <https://github.com/llvm/llvm-project/issues/148180>`__",""
157157
"`P3552R3 <https://wg21.link/P3552R3>`__","Add a Coroutine Task Type","2025-06 (Sofia)","","","`#148182 <https://github.com/llvm/llvm-project/issues/148182>`__",""

libcxx/include/istream

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public:
7070
basic_istream& getline(char_type* s, streamsize n, char_type delim);
7171
7272
basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
73+
basic_istream& ignore(streamsize n, char_type delim); // Since C++26, implemented as a DR
7374
int_type peek();
7475
basic_istream& read (char_type* s, streamsize n);
7576
streamsize readsome(char_type* s, streamsize n);
@@ -172,6 +173,7 @@ template <class Stream, class T>
172173
# include <__type_traits/conjunction.h>
173174
# include <__type_traits/enable_if.h>
174175
# include <__type_traits/is_base_of.h>
176+
# include <__type_traits/is_same.h>
175177
# include <__type_traits/make_unsigned.h>
176178
# include <__utility/declval.h>
177179
# include <__utility/forward.h>
@@ -292,6 +294,10 @@ public:
292294
basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
293295

294296
basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
297+
template <class _Tp = char_type, __enable_if_t<is_same<_Tp, char>::value, int> = 0>
298+
_LIBCPP_HIDE_FROM_ABI basic_istream& ignore(streamsize __n, char_type __delim) {
299+
return ignore(__n, traits_type::to_int_type(__delim));
300+
}
295301
int_type peek();
296302
basic_istream& read(char_type* __s, streamsize __n);
297303
streamsize readsome(char_type* __s, streamsize __n);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// Requires 396145d in the built library.
10+
// XFAIL: using-built-library-before-llvm-9
11+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
12+
13+
// <istream>
14+
15+
// basic_istream& ignore(streamsize n, char_type delim);
16+
17+
#include <cassert>
18+
#include <sstream>
19+
#include <string>
20+
21+
#include "test_macros.h"
22+
23+
int main(int, char**) {
24+
std::istringstream in("\xF0\x9F\xA4\xA1 Clown Face");
25+
in.ignore(100, '\xA1'); // Ignore up to '\xA1' delimiter,
26+
// previously might have ignored to EOF.
27+
28+
assert(in.gcount() == 4); // 4 bytes were ignored.
29+
assert(in.peek() == ' '); // Next character is a space.
30+
31+
std::string str; // Read the next word.
32+
in >> str;
33+
assert(str == "Clown");
34+
35+
// Parameter value "-1L" doesn't cause ambiguity with the char_type overload.
36+
in.ignore(100, -1L); // Ignore up to EOF, which is the default behavior.
37+
assert(in.eof()); // Stream should be at EOF now.
38+
assert(in.gcount() == 5);
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)