Wednesday, September 5, 2012

Should I use printf in my C++ code? - Stack Overflow

Should I use printf in my C++ code? - Stack Overflow

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  :-)    :

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.
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.
share|edit
+1 for the link. It's no 10 Commandments, but they sure have their heads on straight. – ojrac Jan 7 '10 at 5:07
In my opinion while the Google C++ Style Guide is very good in many respects, the consistency trump they refer to is that of their own code. Remember that Google has been around for 10 years, and they value code consistency (a very good thing). The reason they aren't using printf is because people used it in previous incarnations of their code and they want to remain consistent. If this weren't the case I'm confident that they would be using streams instead. – Geoff Mar 12 '10 at 16:59
Huh?! You have precision control with iostreams, too. – phresnel Feb 9 at 13:17
From the referenced link ( http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Streams#Streams ):

Streams

link
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
The compiler does not generate an error because << 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));
And so on and so forth for any issue you might bring up. (You could argue, "Things would be better with the right wrappers," but if it is true for one scheme, is it not also true for the other? Also, remember the goal is to make the language smaller, not add yet more machinery that someone has to learn.)
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
...

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'

No comments:

Post a Comment