得到“返回'auto'的函数在定义之前不能使用”。在C ++ / WinRT项目中使用CoreDispatcher :: RunAsync时 [英] Getting "a function that returns 'auto' cannot be used before it is defined" while using CoreDispatcher::RunAsync in C++/WinRT project

查看:562
本文介绍了得到“返回'auto'的函数在定义之前不能使用”。在C ++ / WinRT项目中使用CoreDispatcher :: RunAsync时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C ++ / WinRT项目中,我试图在UI线程上运行一些代码,但是出现错误消息:


winrt :: impl :: consume_Windows_UI_Core_ICoreDispatcher< winrt :: Windows :: UI :: Core :: ICoreDispatcher> :: RunAsync':返回'auto'的函数在定义之前不能使用


我正在调用这样的方法:

  Dispatcher()。RunAsync(Windows :: UI :: Core :: CoreDispatcherPriority :: Normal,[=]()
{
//要执行的代码。
} );

该实现来自自动生成的winrt文件,该文件返回 auto

  template< typename D> 
结构consumate_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;
};

我是否缺少某些东西,或者这是一个错误?

解决方案

这是相当对C ++ / WinRT库的新添加。在生成的文件中使用返回类型推导可以将用于触发链接器错误的内容转换为编译器错误。出于以下几个原因,编译器错误是有利的:




  • 提早生成错误。您不再需要等待编译器完成,仅需查看链接器在以后的构建过程中就会失败。

  • 编译器可以看到源代码,并且将同时发布文件和导致错误的行号以及类型名称。相比之下,链接器将包含错误的类型名称,从而导致非常嘈杂的输出。



错误诊断的原因丢失了头文件的 #include 指令,其中包含有关类型的完整定义。识别缺少的包含通常很简单。错误消息包括缺少的类型名称,格式如下:


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


相应的头文件位于 winrt 目录下,该目录的名称是点分隔的命名空间串联,后跟 .h



在这种情况下,缺少的类型为


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


所以您需要 #include< winrt / Windows.UI.Core.h> 到使用 ICoreDispatcher 接口的编译单元中。 / p>

Raymond Chen在其名为为什么我的C ++ / WinRT项目会收到 consume_Something:在定义前无法使用返回'auto'的函数形式的错误?


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?

解决方案

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

这篇关于得到“返回'auto'的函数在定义之前不能使用”。在C ++ / WinRT项目中使用CoreDispatcher :: RunAsync时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆