Search code examples
c++lambdawindows-runtimec++-winrt

Ambigious call to overloaded function when using lambda


Never used lambdas before and I can't understand where I'd have to add it.

My Error is "Show: Ambigious call to overloaded function"

Show() can take 2 types CustomizeToast and CustomizeToastAsync. So I guess I need to specify CustomizeToast somewhere but I can't for the life of me see where.

This is my current code:

ToastContentBuilder()
    .AddText(L"Hello World!")
    .Show([](ToastNotification const& toast) -> void
    {
       toast.Tag(L"1");
    });

Error:

error C2668: 'winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show': ambiguous call to overloaded function
Microsoft.Toolkit.Uwp.Notifications.0.h(2159,60): message : could be 'auto winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show(const winrt::Microsoft::Toolkit::Uwp::Notifications::CustomizeToastAsync &) const'
Microsoft.Toolkit.Uwp.Notifications.0.h(2158,31): message : or       'auto winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show(const winrt::Microsoft::Toolkit::Uwp::Notifications::CustomizeToast &) const'
message : while trying to match the argument list '(winrt::MyApplication::implementation::Toast::ShowToastNotification::<lambda_786678859ea03b85c00686eebdcb39db>)'

Project is an UWP project using the Microsoft.Toolkit.Uwp.Notifications


Solution

  • Show() can take 2 types CustomizeToast and CustomizeToastAsync.

    This is clearly an oversight on the API's developpers end. Now since both classes can be constructed from a lambda the compiler doesn't know which one to use, so you have to guide it:

    ToastContentBuilder()
        .AddText(L"Hello World!")
        .Show(CustomizeToast{[](ToastNotification const& toast) -> void
        {
           toast.Tag(L"1");
        }});
    

    This is less readable and cumbersome, but constructing syntaxic suggar for it would be error-prone and a bit convoluted.