microsoft / cppwinrt

C++/WinRT
MIT License
1.64k stars 238 forks source link

Bug: Can't add COM interfaces to implementation base class template of XAML-based class #1309

Closed amtopel closed 1 year ago

amtopel commented 1 year ago

Version

2.0.220110.5

Summary

If you try to add a COM interface to the base class template of a XAML-based class, you will get a static assert error. This is the case even if you include unknwn.h before any C++/WinRT headers.

Reproducible example

In the WinUI 3 blank project, modify the MainWindow class so that it tries to implement a COM interface, e.g., IInitializeWithWindow:


#include <Shobjidl.h>

namespace winrt::App1::implementation
{
    struct MainWindow : MainWindowT<MainWindow, ::IInitializeWithWindow>
    {
        MainWindow()
    {
             InitializeComponent();
    }

        IFACEMETHOD(Initialize)(HWND hwnd)
        {
             return S_OK;
        }
    };
}

Expected behavior

I'd expect the code to compile.

Actual behavior

Error: static_assert failed: "Class must derive from implements<> or ClassT<> where the first template parameter is the derived class name, e.g. struct D : implements<D, ...>");

Additional comments

No response

sylveon commented 1 year ago

This has already been filed over at the WinUI repo: https://github.com/microsoft/microsoft-ui-xaml/issues/3331