获取“在定义之前不能使用返回'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

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

问题描述

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

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':在定义之前不能使用返回'auto'的函数"

"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.
});

该实现来自自动生成的 winrt 文件,该文件返回 auto 作为返回类型.

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?

推荐答案

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

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:

  • 构建错误很早就出现了.您不再需要等待编译器完成,只会在构建过程中稍后看到链接器失败.
  • 编译器可以看到源代码,并且会发出导致错误的文件和行号以及类型名称.相比之下,链接器将包含损坏的类型名称,从而导致非常嘈杂的输出.

错误诊断的原因是头文件缺少 #include 指令,该指令包含相关类型的完整定义.识别丢失的包含通常很简单.错误消息包括缺少的类型名称,采用以下形式

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___..._

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

相应的头文件在 winrt 目录下,其名称是命名空间的点分隔连接,后跟 .h.

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::impl::consume_Windows_UI_Core_ICoreDispatcher<winrt::Windows::UI::Core::ICoreDispatcher>

因此您需要将 #include 加入到使用 ICoreDispatcher 接口的编译单元中.

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

Raymond Chen 在其名为 为什么的博客条目中提供了有关该主题的更多背景信息我的 C++/WinRT 项目是否会收到consume_Something:在定义之前不能使用返回‘auto’的函数"形式的错误?.

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天全站免登陆