何时使用 std::invoke 而不是简单地调用可调用对象? [英] When to use std::invoke instead of simply calling the invokable?

查看:52
本文介绍了何时使用 std::invoke 而不是简单地调用可调用对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,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屋!

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