In C++, to print a number in hexadecimal you do this:
int num = 10;
std::cout << std::hex << num; // => 'a'
I know I can create a manipulator that just adds stuff to the stream like so:
std::ostream& windows_feed(std::ostream& out)
{
out << "\r\n";
return out;
}
std::cout << "Hello" << windows_feed; // => "Hello\r\n"
However, how can I create a manipulator that, like 'hex', modifies items to come on the stream? As a simple example, how would I create the plusone manipulator here?:
int num2 = 1;
std::cout << "1 + 1 = " << plusone << num2; // => "1 + 1 = 2"
// note that the value stored in num2 does not change, just its display above.
std::cout << num2; // => "1"
You'll have to play with streamstates. I've bookmarked the following links on the subject:
As Maciej Sobczak library is no longer available online, and as the licence permits me to do so, (correct me if I'm wrong), here is a copy of its main file that I've managed to salvage from oblivion:
I totally agree with Neil Butterworth on this one, however in the specific case you are using you could do this totally horrible hack. Do not do this in any production code. It has lots of bugs. For one thing it only works in your one-liner above, it does not change the state of the underlying stream.
It's not a direct answer to your question, but don't you think that using a plain old function is both simpler to implement and clearer to use than writing a full blown manipulator?
Usage:
By "clear to use", I mean that the user doesn't need to ask themselves, "Does it affect only the next item, or all subsequent items?" It's obvious from inspection that only the argument of the function is affected.
(For the
plusone()
example, you could simplify even further by just returning aT
instead, but returning astd::string
serves the general case.)First, you have to store some state into each stream. You can do that with the function
iword
and an index you pass to it, given byxalloc
:Having that in place, you can already retrieve some state in all streams. Now, you just have to hook into the respective output operation. Numeric output is done by a facet, because it potentially is locale dependent. So you can do
Now, you can test the stuff.
If you want that only the next number is incremented, just set the word to
0
again after each call todo_put
.I created a simple solution for your test case without using
<iomanip>
. I can't promise that the same approach will work in real life.The basic approach is that
cout << plusone
returns a temporary auxiliary object (PlusOnePlus
), which in turn has the overloadedoperator <<
that performs the addition.I've tested it on Windows:
produces "42", as expected. Here's the code:
EDIT: Commented the code to point out that I'm relying on the default copy constructor (etc.) for
PlusOnePlus
. A robust implementation would probably define theselitb's approach is "the right way" and necessary for complicated stuff, but something like this can be good enough. Add privacy and friendship to taste.
In this simple example creating and streaming a temporary object doesn't seem much more helpful than defining a function plusOne() as someone already suggested. But suppose you wanted it to work like this: