std::ws(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME

std::ws β€” okay so it's literally std::ws, bestie, that's the whole name 🫢 (and yes, the vibe is "eat the whitespace, leave the good part")

From the C++ Standard Library, no cap. ✨


✨ SYNOPSIS

It's defined in the header <istream>, and here's the signature, exactly as written, don't come for me:

template< class CharT, class Traits >
std::basic_istream<CharT, Traits>& ws( std::basic_istream<CharT, Traits>& is );

So what does it do? It discards leading whitespace from an input stream. That's the whole personality. πŸŽ€

It behaves as an UnformattedInputFunction, except that is.gcount() is not modified. (Yes, that exception matters. Remember it. πŸ’…)

After constructing and checking the sentry object, it extracts characters from the stream and discards them until any one of these things happens:

  • an end of file condition occurs in the input sequence β€” in which case the function calls setstate(eofbit) but does not set failbit. ⚠️ This does not apply if eofbit is already set on is prior to the call to ws, because in that case the construction of the sentry object would set failbit. (Messy, I know. That's the rule though, fr fr.)
  • the next available character c in the input sequence is not whitespace, as determined by std::isspace(c, is.getloc()). That non-whitespace character is not extracted β€” it stays right where it is, main character energy. πŸ‘‘

It's an input-only I/O manipulator, so you can call it in an expression like in >> std::ws for any in of type std::basic_istream. πŸ¦‹


🌸 PARAMETERS

  • is β€” reference to the input stream. That's it. That's the parameter. πŸ“

πŸ’– RETURN VALUE

Returns is β€” a reference to the stream, after it ate all those consecutive whitespace characters. Slay. ✨


🧚 NOTES

If eofbit is set on the stream prior to the call, the construction of the sentry object will set failbit. So like… heads up. πŸ™ˆ


πŸ”₯ EXAMPLE

// Run this code

#include <iomanip>
#include <iostream>
#include <istream>
#include <sstream>
#include <string>

int main()
{
    for (const char* str : {"     #1 test", "\t #2 test", "#3 test"})
    {
        std::string line;
        std::getline(std::istringstream{str}, line);
        std::cout << "getline returns:\t" << std::quoted(line) << '\n';

        std::istringstream iss{str};
        std::getline(iss >> std::ws, line);
        std::cout << "ws + getline returns:\t" << std::quoted(line) << '\n';
    }
}

⭐ OUTPUT

getline returns:        "     #1 test"
ws + getline returns:   "#1 test"
getline returns:        "        #2 test"
ws + getline returns:   "#2 test"
getline returns:        "#3 test"
ws + getline returns:   "#3 test"

See how the whitespace just… vanished? Iconic. πŸ’…βœ¨


🌈 DEFECT REPORTS

These are the behavior-changing defect reports that got applied retroactively to previously published C++ standards β€” yes, time travel, we love to see it. πŸ•°οΈ

  • LWG 415 β€” applied to C++98.
    • Behavior as published: calling std::ws might not construct the sentry object (inconsistent with other input functions).
    • Correct behavior: it's required to construct the sentry object. πŸ‘‘

πŸ¦‹ SEE ALSO

  • ignore β€” extracts and discards characters until the given character is found (public member function of std::basic_istream<CharT,Traits>). Different vibe, same "throw it away" energy. πŸ—‘οΈ

okay that's the whole page, you ate that up honestly πŸ‘‘πŸ’– β€” this lil writeup came from cppreference.com, dated 2024.06.10, and the manual page is std::ws(3). go forth and strip that whitespace, bestie 🌸✨🫢

πŸ“¦ where this page comes from