Using Printf with Modern C++

Here’s my article in the March 2015 issue of MSDN Magazine:

Using Printf with Modern C++

What would it take to modernize printf? That might seem like an odd question to many developers who believe that C++ already provides a modern replacement for printf. While the claim to fame of the C++ Standard Library is undoubtedly the excellent Standard Template Library (STL), it also includes a stream-based input/­output library that bears no resemblance to STL and embodies none of its principles related to efficiency.

This is partly in response to a brief defense of printf that I published late last year. Enjoy!

2 thoughts on “Using Printf with Modern C++

  1. Will Hayward

    Hi Kenny. I just tried the code from your “Printf with Modern C++” article in a log-writing utility class and found that your StringPrint() function, as printed, is not compatible with your “non-naive” Format() function. For one, StringPrint() returns an int while Format() appears to expect a size_t. More importantly though, if you try to pass an empty std::wstring into Format(), similar to your ToString() example, you end up asserting right away in Format()’s first call to StringPrint(), when swprintf() returns -1. I wonder if you had a different implementation of StringPrint in mind for your final version of Format()? Perhaps Format() should use _scwprintf() to get the required size, then call StringPrint().

    1. Kenny Kerr Post author

      This is a very simple approach that I presented mainly to get developers thinking about printf and modern C++. It has a number of shortcomings. You should be able to get it to work with minimal effort. I present a much more sophisticated approach in the May 2015 issue (that will be online in the next few days).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s