何时使用 std::invoke 而不是简单地调用可调用对象? [英] When to use std::invoke instead of simply calling the invokable?
问题描述
据我所知,std::invoke
允许我执行以下操作:
As I understand, std::invoke
allows me to do something like:
std::invoke(f, arg1, arg2, ...);
有没有比简单地做更有利的场景:
Is there a scenario when it's more advantageous than simply doing:
f(arg1, arg2, ...);
推荐答案
如果 invocable 是指向成员函数的指针,那么您需要执行以下操作之一:
If the invocable is a pointer to a member function, then you need to do one of these:
(arg1->*f)(arg2,...);
(arg1.*f)(arg2,...);
取决于 arg1
是什么.
INVOKE(和它的官方库对应std::invoke
) 几乎旨在简化此类混乱.
INVOKE (and its official library counterpart std::invoke
) was pretty much designed to simplify such messes.
您将使用 std::invoke
来支持您的代码的调用者传递任何可调用,而不必使用 lambda 或调用来调整他们的调用站点到 std::bind
.
You'd use std::invoke
to support the caller of your code passing any callable, and not having to adapt their call site with a lambda or a call to std::bind
.
这篇关于何时使用 std::invoke 而不是简单地调用可调用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!