May 4th, 2022

Producing an empty Windows Runtime type in C++/WinRT

Windows Runtime types fall into two general categories: Reference types and value types.

Reference types are copied by reference. When you copy a reference type, you’re really just incrementing the reference count on the underlying object. And Windows Runtime references can be null.

Value types are copied by value. When you copy a value type, you are making a shallow copy of everything inside it. Windows Runtime values cannot be null.

There are cases where you want to produce an empty Windows Runtime type. For reference types, this would be a null reference, and for value types, it would be a default-initialized value filled with zero, false, and null, as appropriate.

In other words, we want to replicate what C# default(T) does.

Using {} to generate an empty value works for delegates, interfaces, and value types, but not for runtime classes, because C++/WinRT expresses the default constructor of the runtime class as the default constructor of the projection. In other words, default-constructing a Windows Runtime class in C++/WinRT actually creates an object, so you don’t get nullptr at all. You get a real live object.

We need to detect the case where we have a Windows Runtime runtime class and use the nullptr constructor in that case.

The shortcut here is that the nullptr constructor also works for delegates and interfaces, we can just use it for all reference types.

And that leads us to this:

template<typename T>
constexpr T winrt_empty_value() noexcept
{
    if constexpr (std::is_base_of_v<winrt::Windows::Foundation::IUnknown, T>) {
        return nullptr;
    } else {
        return {};
    }
}

Making the whole thing constexpr allows it to be used a lot of places that would normally be constrained to constants.

This function will come in handy later, but we’ll have to get there first.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.