Category Archives: Uncategorized

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!

Modern + Asynchronous Programming

I recently added explicit support for asynchronous programming to Modern C++ for the Windows Runtime. It now supports both PPL tasks as well as the new await keyword proposed for the C++ programming language to implement stackless coroutines (known as resumable functions). You can find more information along with a short video demo here:

Asynchronous Programming

The Modern C++ Library provides a Standard C++ language projection for the Windows Runtime. But what about asynchronous programming? Isn’t that ubiquitous in the new Windows API? It sure is and Modern has you covered.

SQLite with Modern C++

My latest Pluralsight course is now available:

SQLite with Modern C++

Learn everything you need to know to make great use of this incredible little database engine with modern C++.

This course introduces C++ developers to the SQLite database engine, an embeddable and completely self-contained database engine written entirely in C. The benefit for C++ developers is that there is no configuration or distribution headaches. The drawback is that the API is provided in C. This course will show developers how to effectively and efficiently wrap the C API in a modern C++ abstraction that adds no runtime cost but greatly improves reliability and productivity.

Go and watch it now!

Many thanks to Richard Hipp for answering my questions as I was preparing for this course!

COM Smart Pointers Revisited

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

COM Smart Pointers Revisited

After the second coming of COM, otherwise known as the Windows Runtime, the need for an efficient and reliable smart pointer for COM interfaces is more important than ever. But what makes for a good COM smart pointer? The ATL CComPtr class template has been the de facto COM smart pointer for what feels like decades. The Windows SDK for Windows 8 introduced the ComPtr class template as part of the Windows Runtime C++ Template Library (WRL), which some hailed as a modern replacement for the ATL CComPtr. At first, I also thought this was a good step forward, but after a lot of experience using the WRL ComPtr, I’ve come to the conclusion it should be avoided.

This is another example from Modern C++ for the Windows Runtime. It doesn’t play a central role in the library but comes in handy when you need to interop with other APIs not covered by the Modern compiler. You can read about one of the key building blocks of the Modern project in this article where I discuss the implementation of IUnknown and IInspectable with variadic templates.

Modern Source Code Size and Internal Consistency

Work continues on Modern C++ for the Windows Runtime as I have time to spare. I recently replaced enumerator values with literals to save over 200KB in the resulting library. Previously, I played it safe and simply used the underlying ABI for enumerator values:

enum class ApplicationExecutionState
{
    NotRunning = ABI::Windows::ApplicationModel::Activation::ApplicationExecutionState_NotRunning,
    Running = ABI::Windows::ApplicationModel::Activation::ApplicationExecutionState_Running,
    Suspended = ABI::Windows::ApplicationModel::Activation::ApplicationExecutionState_Suspended,
    Terminated = ABI::Windows::ApplicationModel::Activation::ApplicationExecutionState_Terminated,
    ClosedByUser = ABI::Windows::ApplicationModel::Activation::ApplicationExecutionState_ClosedByUser,
};

The Modern compiler now simply generates enumerations as follows:

enum class ApplicationExecutionState
{
    NotRunning = 0,
    Running = 1,
    Suspended = 2,
    Terminated = 3,
    ClosedByUser = 4,
};

Apart from the dramatic reduction in code size, it also benefits compile times, reduces the work required by the struggling IntelliSense compiler, and generates smaller precompiled and IntelliSense databases.

I then added an optional -debug command line option to add static_assert consistency checks, adding over 3,000 compile-time assertions. Of course, this goes beyond simply checking enumerator values. It’s not something you’ll want to enable all of the time. Instead, it’s a handy way to quickly verify whether the language projection for a new version of the Windows SDK (e.g. Windows 10) is internally consistent with the underlying ABIs.

Visual C++ 2015 Brings Modern C++ to the Windows API

MSDN Magazine released a special issue for Microsoft’s Connect(); event in New York City. I was asked to write a C++ feature and you can now read it online:

Visual C++ 2015 Brings Modern C++ to the Windows API

Visual C++ 2015 is the culmination of a huge effort by the C++ team to bring modern C++ to the Windows platform. Over the last few releases, Visual C++ has steadily gained a rich selection of modern C++ language and library features that together make for an absolutely amazing environment in which to build universal Windows apps and components. Visual C++ 2015 builds on the remarkable progress introduced in those earlier releases and provides a mature compiler that supports much of C++11 and a subset of C++14. You might argue about the level of completeness, but I think it’s fair to say that the compiler supports the most important language features, enabling modern C++ to usher in a new era of library development for Windows. And that really is the key. As long as the compiler supports the development of efficient and elegant libraries, developers can get on with building great apps and components.

If you like variadic templates then you’ll want to read this. It also offers a glimpse of some the techniques used to power Modern C++ for the Windows Runtime (moderncpp.com).

In defense of printf

Folks seem to enjoy pointing out that I use printf in many of my examples of “modern C++”, as if printf is not really proper C++. Apparently, I should be using cout. The trouble is that cout isn’t exactly modern either. It has been around for as long as I can remember and it certainly doesn’t exemplify modern C++ as envisioned by C++11 and beyond. The oldest C++ textbook on my shelf was printed in 1993 and covers cout. I even posed the question to some C++ historians and they were able to date it back as far as 1989. Therefore, the argument that printf is old and cout is modern doesn’t fly. A truly modern C++ solution would also not be substantially slower than hand-written code. Most printf implementations today provide adequate type checking both at compile time and run time. Visual C++ even provides secure versions that make it quite straightforward to write defensive code quite easily with printf. Go ahead and use cout if you prefer, but don’t claim it’s the modern replacement for printf.

Here’s a slide from my 10 Practical Techniques to Power Your Visual C++ Apps course where I examine the performance of searching and sorting text. I won’t explain the numbers – you can watch the course for that – but it should be evident that cout has a serious performance problem. That analysis was done before James McNellis added some awesome modernization and performance improvements to printf in Visual C++ 2015.

cout