Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason why the stream cout has the flag std::ios_base::skipws set?

Tags:

c++

iostream

cout has the flags std::ios_base::skipws and std::ios_base::dec set by default

You can verify this with the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    ios_base::fmtflags flags = cout.flags();
    string sflags;

    if( flags & ios_base::skipws ) sflags += "skipws";
    if( flags & ios_base::unitbuf ) sflags += sflags.empty() ? "unitbuf" : " unitbuf";
    if( flags & ios_base::uppercase ) sflags += sflags.empty() ? "uppercase" : " uppercase";
    if( flags & ios_base::showbase ) sflags += sflags.empty() ? "showbase" : " showbase";
    if( flags & ios_base::showpoint ) sflags += sflags.empty() ? "showpoint" : " showpoint";
    if( flags & ios_base::showpos ) sflags += sflags.empty() ? "showpos" : " showpos";
    if( flags & ios_base::left ) sflags += sflags.empty() ? "left" : " left";
    if( flags & ios_base::right ) sflags += sflags.empty() ? "right" : " right";
    if( flags & ios_base::internal ) sflags += sflags.empty() ? "internal" : " internal";
    if( flags & ios_base::dec ) sflags += sflags.empty() ? "dec" : " dec";
    if( flags & ios_base::oct ) sflags += sflags.empty() ? "oct" : " oct";
    if( flags & ios_base::hex ) sflags += sflags.empty() ? "hex" : " hex";
    if( flags & ios_base::scientific ) sflags += sflags.empty() ? "scientific" : " scientific";
    if( flags & ios_base::fixed ) sflags += sflags.empty() ? "fixed" : " fixed";
    if( flags & ios_base::hexfloat ) sflags += sflags.empty() ? "hexfloat" : " hexfloat";
    if( flags & ios_base::boolalpha ) sflags += sflags.empty() ? "boolalpha" : " boolalpha";
    if( flags & ios_base::_Stdio ) sflags += sflags.empty() ? "_Stdio" : " _Stdio";

    cout << "Standard flags from cout stream: " << sflags << endl;
}

Clearly the flag std::ios_base::skipws is irrelevant for cout.

like image 985
Belloc Avatar asked Dec 30 '12 17:12

Belloc


2 Answers

The flags and their default setting is inherited from std::ios_base (well, actually, the settings are defined for std::basic_ios<cT, Traits>; the defaults are define in 27.5.5.2 [basic.ios.cons]), a common base class of both input and output streams. The flags are shared if the stream inherits from both input and output streams. There are other flags which don't make much sense on either input or output streams.

like image 181
Dietmar Kühl Avatar answered Nov 02 '22 10:11

Dietmar Kühl


The skipws flag is set in all standard streams on initialization. Not just std::cout. It makes as much sense for std::cout as for any other stream. You can disable it with noskipws if you hate it so much.

like image 44
Lightness Races in Orbit Avatar answered Nov 02 '22 10:11

Lightness Races in Orbit