Defined in header <cparse/Cursor.hh>
stream_type::char_type safePeek() noexcept;
Same as peek() but catches every exception and returns the EOF sentinel '\032' (^Z) if the stream cannot be inspected.
Explicitly clears the eof bit before peeking. This matters: after an EOF condition, a plain peek() would keep throwing. safePeek() resets the eof state, tries a fresh peek, and returns the sentinel if the peek fails or the stream is still at EOF.
Parameters
None.
Return value
The next character in the stream on success. '\032' if the stream pointer is null, if the underlying peek() throws, or if the stream is at EOF after the state clear.
Exceptions
noexcept. All exceptions from the underlying stream are caught and translated to the sentinel return value.
Notes
The EOF sentinel is described in safeGet Notes.
safePeek() is what you want in almost every look-ahead in a grammar, because it can appear inside condition expressions without a surrounding try:
if (getCursor().safePeek() == '(') // safe even at EOF
getCursor().get();
Example
#include <cparse/Cursor.hh>
#include <sstream>
#include <cctype>
int main()
{
auto ss = std::make_shared<std::istringstream>("xy");
fedem::parser::Cursor c("<memory>", ss);
c.safeGet(); c.safeGet(); // consume both characters
// Stream is now at EOF. safePeek() clears eof, tries, sees no data,
// returns the sentinel — never throws.
auto ch = c.safePeek();
// ch == '\032'
}
See also
- Cursor::peek — throwing variant.
- Cursor::safeGet — inspect and consume, non-throwing.

