I was just wondering what people really do if they have to print a lot of hex in their C++ code, and if in addition, they say always want to see a minimum width, which I find ugly and very verbose and error-prone with the cout/streams format.
(e.g., std::cout << std::hex << std::setfill('0') << std::setw(8) << x << std::dec << std::endl; )
I feel like there must be a good third solution, or people just use printf, or ???. I read the above article, and about Boost.Format and all, which looks sorta interesting (although the examples they show don't seem all that brilliant for hex either, to me). And people point out that for streams, "endl" implies a flush(), which you would otherwise have to do manually... But maybe I liked this answer best :-) :
3 |
Use printf. Do not use C++ streams. printf gives you much better control (such as float precision etc.). The code is also usually shorter and more readable.
Google C++ style guide agrees.
| ||||||
|
Streams
Use streams only for logging.
Definition:Streams are a replacement for
printf()
and scanf()
.
Pros:With streams, you do not need to know the type of the object you are printing. You do not have problems with format strings not matching the argument list. (Though with gcc, you do not have that problem with
printf
either.) Streams have automatic constructors and destructors that open and close the relevant files.
Cons:Streams make it difficult to do functionality like
pread()
. Some formatting (particularly the common format string idiom %.*s
) is difficult if not impossible to do efficiently using streams without using printf
-like hacks. Streams do not support operator reordering (the %1s
directive), which is helpful for internationalization.
Decision:
Do not use streams, except where required by a logging interface. Use printf
-like routines instead.There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.
Extended Discussion
There has been debate on this issue, so this explains the reasoning in greater depth. Recall the Only One Way guiding principle: we want to make sure that whenever we do a certain type of I/O, the code looks the same in all those places. Because of this, we do not want to allow users to decide between using streams or using printf
plus Read/Write/etc. Instead, we should settle on one or the other. We made an exception for logging because it is a pretty specialized application, and for historical reasons.Proponents of streams have argued that streams are the obvious choice of the two, but the issue is not actually so clear. For every advantage of streams they point out, there is an equivalent disadvantage. The biggest advantage is that you do not need to know the type of the object to be printing. This is a fair point. But, there is a downside: you can easily use the wrong type, and the compiler will not warn you. It is easy to make this kind of mistake without knowing when using streams.
cout << this; // Prints the address cout << *this; // Prints the contents
<<
has been overloaded. We discourage overloading for just this reason.Some say
printf
formatting is ugly and hard to read, but streams are often no better. Consider the following two fragments, both with the same typo. Which is easier to discover?cerr << "Error connecting to '" << foo->bar()->hostname.first << ":" << foo->bar()->hostname.second << ": " << strerror(errno); fprintf(stderr, "Error connecting to '%s:%u: %s", foo->bar()->hostname.first, foo->bar()->hostname.second, strerror(errno));
Either path would yield different advantages and disadvantages, and there is not a clearly superior solution. The simplicity doctrine mandates we settle on one of them though, and the majority decision was on
printf
+ read
/write
....
If you need some further ammo, I also liked this guy, he is like a C++ anarchist, but he has some fair points:
http://yosefk.com/c++fqa/io.html#fqa-15.1
...
http://yosefk.com/c++fqa/io.html#fqa-15.1
...
FQA: Why should I do this, why should I do that, you ask. What kind of manners are these? Do what you are told. Assignment Number 1 - convert all your evil printf("0x%08xn", x)statements to this:
std::cout << std::hex << std::setfill('0') << std::setw(8) << x << std::dec << std::endl;
Even if you commit the environmental crime of namespace pollution, adding a using namespace std and removing those pesky std::, the verbosity is still amazing. This achievement is not accidental. It follows from one of the basic flaws in the C++ way of thinking: the "everything is a type" axiom. For example, hex has a special type which hexes streams, and so does every other strange object sent to cout.
The FAQ explains why this thinking is good for you. Here's why the FAQ is wrong:
...
;-)
Happy Coding,
Connie
'via Blog this'