Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

uwp - Getting "a function that returns 'auto' cannot be used before it is defined" while using CoreDispatcher::RunAsync in C++/WinRT project

In my C++/WinRT project, I am trying to run some code on UI thread but getting an error that says:

"winrt::impl::consume_Windows_UI_Core_ICoreDispatcher<winrt::Windows::UI::Core::ICoreDispatcher>::RunAsync': a function that returns 'auto' cannot be used before it is defined"

I am invoking the method like this:

Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [=]()
{
     // Code to be executed.
});

The implementation is coming from auto generated winrt file which returns auto as a return type.

template <typename D>
struct consume_Windows_UI_Core_ICoreDispatcher
{
    [[nodiscard]] auto HasThreadAccess() const;
    auto ProcessEvents(Windows::UI::Core::CoreProcessEventsOption const& options) const;
    auto RunAsync(Windows::UI::Core::CoreDispatcherPriority const& priority, Windows::UI::Core::DispatchedHandler const& agileCallback) const;
    auto RunIdleAsync(Windows::UI::Core::IdleDispatchedHandler const& agileCallback) const;
};

Is there something that I am missing or is this a bug?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the result of a fairly new addition to the C++/WinRT library. Using return type deduction in the generated files turns what used to trigger a linker error into a compiler error. A compiler error is favorable for several reasons:

  • The build errors out early. You no longer have to wait for the compiler to finish, only to see the linker fail later in the build process.
  • The compiler can see the source code, and will issue both the file and line number responsible for the error, alongside the type name. By contrast, the linker will include the mangled type name, leading to very noisy output.

The reason for the error diagnostic is a missing #include directive for the header file that contains the full definition of the type in question. Identifying the missing include is usually straight forward. The error message includes the missing type name, taking the following form

winrt::impl::consume_<namespace1>_<namespace2>_..._<some_interface>

The respective header file is underneath the winrt directory, whose name is the dot-separated concatenation of namespaces, followed by .h.

In this case the missing type is

winrt::impl::consume_Windows_UI_Core_ICoreDispatcher<winrt::Windows::UI::Core::ICoreDispatcher>

so you need to #include <winrt/Windows.UI.Core.h> into the compilation unit that's using the ICoreDispatcher interface.

Raymond Chen has more background information on the topic in his blog entry titled Why does my C++/WinRT project get errors of the form “consume_Something: function that returns ‘auto’ cannot be used before it is defined”?.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...