什么是C ++中的std :: invoke? [英] What is std::invoke in c++?

查看:2671
本文介绍了什么是C ++中的std :: invoke?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读到我遇到的 std :: thread std :: bind 具有 Callable 概念和 std :: invoke 的概念。

I've just been reading about std::thread and std::bind which I've faced with the Callable concept and std::invoke.

我在std :: invoke 的信息/ w / cpp / utility / functional / invoke rel = noreferrer> cppreference ,但我不明白它在说什么。这是我的问题:

什么是 std :: invoke std :: function std :: bind Callable 概念?它们之间是什么关系?

I read about std::invoke on cppreference but I didn't understand what it said. Here is my question:
What is std::invoke, std::function, std::bind and the Callable concept? And what is relationship between them?

推荐答案

std :: invoke 需要可调用的东西,以及用于调用它的参数,然后进行调用。 std :: invoke(f,args ...)是键入 f(args ...)

std::invoke takes something callable, and arguments to call it with, and does the call. std::invoke( f, args... ) is a slight generalization of typing f(args...) that also handles a few additional cases.

可调用项包括函数指针或引用,成员函数指针,带有 operator( )或指向成员数据的指针。

Something callable includes a function pointer or reference, a member function pointer, an object with an operator(), or a pointer to member data.

在成员情况下,第一个参数解释为 this 。然后将其余参数通过 std :: reference_wrapper ()(在指向成员数据的指针的情况下除外)。

In the member cases, the first argument is interpreted as the this. Then remaining arguments are passed to () (except in the pointer-to-member-data-case), with std::reference_wrappers unwrapping.

INVOKE是C ++标准中的一个概念; C ++ 17只是公开了直接执行它的 std :: invoke 。我怀疑它是公开的,部分原因是它在进行其他元编程时很有用,部分原因是每个标准库已经在其中实现了INVOKE的实现,并公开了它基本上是免费的,部分是因为在具体的事情上,使INVOKE的讨论变得更加容易

INVOKE was a concept in the C++ standard; C++17 simply exposed a std::invoke which does it directly. I suspect it was exposed partly because it is useful when doing other metaprogramming, partly because every standard library has an implementation of INVOKE in it already and exposing it was basically free, and partly because it makes talking about INVOKE easier when it is a concrete thing.

这篇关于什么是C ++中的std :: invoke?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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