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

别等时光非礼了梦想. 提交于 2019-12-02 07:31:06

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”?.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!