I understood that we can use both options below to change the boolalpha flag, but why do we need to qualify ios_base in the setf() function and can't use std::boolalpha within the function parameter?
std::cout << std::boolalpha; //using manipulator
std::cout.setf(ios_base::boolalpha); // we can't use std::cout.setf(std::boolalpha);
std::boolalpha is a stream function object (also known as a manipulator) that sets the boolalpha flag on a stream. This flag causes boolean values to be written out as "true" or "false" rather than as "1" or "0".
The setf() function is a member function of the std::ios_base class, which is the base class for all stream classes. The setf() function is used to set various formatting flags on a stream, including the boolalpha flag. The first argument to setf() is an enumeration value that specifies which flag to set.
In this case, ios_base::boolalpha is the enumeration value that represents the boolalpha flag, so std::cout.setf(ios_base::boolalpha) sets the boolalpha flag on the std::cout stream.
The reason you can't use std::boolalpha within the function parameter, is that std::boolalpha is a manipulator, and setf() expects the enumeration type as its argument, not a manipulator.
Additionally, a manipulator can be used to simply change the flag, unlike setf() that is meant to be used as a more powerful and general set of operations.
Related
I'm trying to write my own stream class. It will do something with the input and pass it on to std::cout. So far I passed functions operating on the stream (e.g. std::endl) on to cout. Now, I want to check for std::endl input and perform some actions, if it occurs. I have implemented the behaviour like this
DebugStream& DebugStream::operator<< (ostream& (*pfun)(ostream&))
{
if(pfun == std::endl)
{
// do something
}
pfun(cout);
return *this;
}
Which results into
/path/to/file.cpp:89:24: error: assuming cast to type ‘std::basic_ostream<char>& (*)(std::basic_ostream<char>&)’ from overloaded function [-fpermissive]
using the GCC compiler. I don't know, how to compare the function pointer against the std::endl function. Maybe this problem is related to the template nature of the std::endl function? Or because it's inline?
The reason you get a warning is that std::endl is a template function. You can fix this error by forcing a comparison to a template instance of a particular type through a cast, like this:
typedef ostream& (*io_manip_ptr_t)(ostream&);
void demo(io_manip_ptr_t pfun) {
if (pfun == (io_manip_ptr_t)&endl) {
cout << "Hi" << endl;
}
}
Demo.
Note: Even though the work-around is available, this is only a workaround, not a solid solution, because comparing function pointers to decide on functionality introduces a very serious problem: your functions get reduced to an enum, so your code becomes impossible to extend from the outside.
I want to know actual difference between cout<<cout and cout<<&cout in c++? In my compiler cout<<cout returns 0x477864 & cout<<&cout returns 0x477860 at any time.It shows it has 1 digit of difference between them.What are the significance of these?
When you do this:
cout << cout;
You are relying on the stream's implicit conversion to void*. This value is used (pre-c++11) for testing the state of the stream. It is unspecified what the value actually is, it just needs to be NULL if the stream is in a fail state, and a non NULL otherwise. Maybe it's returning the address of a member of the object, but it's not really important, and is implementation defined.
When you do this:
cout << &cout;
That is getting the actual address of cout.
Note that in C++11 and beyond, the first one, cout << cout;, will no longer compile, because the implicit conversion to void* no longer exists. Instead there is an explicit conversion to bool to serve the same purpose.
What is the difference between ios::base:precision & setprecision
Given in the following links?
http://www.cplusplus.com/reference/ios/ios_base/precision/
http://www.cplusplus.com/reference/iomanip/setprecision/
The difference it to do with the usage.
The first (setter) is a member function, so gets called like this:
std::cout.precision(10);
The second isn't a member function so is called different.
std::cout << std::setprecision(10);
The 2nd link explicitly says
"Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator "
Citing your second link:
Behaves as if member precision were called with n as argument on the
stream on which it is inserted/extracted as a manipulator (it can be
inserted/extracted on input streams or output streams).
Thus, there's no difference.
This has been puzzling me for a while: with printf, you provide formmatters to dictate how a parameter should be interpreted, but cout doesn't require this. How does cout know to read a variable according to the correct type?
It's called function overloading. In C++, you can have as many functions as you want with the same name (operator<< is the name in this case), as long as they take different parameter sets. cout doesn't dictate how the parameters are interpreted, the compiler does. Or rather, the compiler dictates which function is called, and that function dictates how the parameter is formatted. The compiler knows the type of each parameter, and calls the appropriate function accordingly.
For example, this calls ostream::operator<<(int)
cout << 10;
And this calls ostream::operator<<(double), which is a completely different function
cout << 3.14;
I am learning operator overloading. "out" is being used instead of "cout" when overloading "<<" operator. I don't understand why.
ostream &operator<<( ostream &out, const IntList &L ) {
out << "[ ";
for (int k=0; k< L.numItems; k++) {
out << L.Items[k] << ' ';
}
out << ']';
}
I want to ask differences between cout and out and what happens if I use cout instead of out.
Thanks for answers.
What you are looking at is a overloaded "stream insertion" operator, allowing some custom class to be written to an ostream object using the typical cout << myObject syntax.
The variable in this case is called out because that's the name they've given to the ostream object being passed into the function, which may be any output stream, whether it's cout or an fstream or a stringstream. It's just a variable name, and they could have called it blah and written:
ostream &operator<<( ostream &blah, const IntList &L ) {
blah << "[ ";
// ...
}
Typically you choose a variable name which is descriptive, and out as a name for an output stream is pretty descriptive.
cout would be an especially bad variable name, as it is strongly associated with std::cout, used for writing specifically to the standard output stream. This code doesn't write specificially to standard output, it writes to any ostream object via the << operator so they've chosen a more generic name for their ostream argument.
I want to ask differences between cout and out and what happens if I use cout instead of out. Thanks for answers.
In this case, an ostream& (out) is a parameter passed to the function. This allows the operator<< to work on any ostream.
cout is a specific ostream instance - the standard output stream. If they used cout here, you wouldn't be able to use the << operator on cerr (the standard error stream) or any other ostream. If you replaced the out with cout in the body, any time you used this on a different ostream, it'd be written to cout. (Of course, if you changed the parameter to be named cout, that wouldn't happen - but it would be very misleading to anybody looking at this code, as people would expect that the code writes to the standard output stream, not to the stream being passed in.)
In general, you only would want to use cout as a name if you are specifically referring to std::cout - the standard output stream, as using it in other contexts would be very confusing.
out is the name of the ostream object passed to the overloaded operator (inside the implementation of the operator).
The overloaded operator allows you to write code like this
IntList i;
cout<<i;
or
cerr<<i;
In the implementation if you substituted out with cout, then the second call
cerr<<i;
would print to standard output whereas it should have printed to standard error.
The critical thing here is really the types in the function signature: as long as it's a freestanding function with two parameters - one of type std::ostream& and the other able to be matched by the value to be streamed, then the function body will be invoked. It should return a reference to the stream to allow chaining (as in if (cout << firstIntList << secondIntList)).
The actual parameter names are whatever you feel like, as long as they're not reserved words. I tend to use "os", as in output-stream.