dummy()函数-应该是什么? [英] dummy() function - What is that supposed to be?

查看:459
本文介绍了dummy()函数-应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处阅读了这个问题,偶然发现了投票得最好的答案,该答案使用了这样的代码来称呼lambda递归:

I read this question here on SO and stumbled over the best voted answer, which used code like this to call a lambda recursively:

std::function<void(int)>
    f {[&f](int i){
        // do something
    }},
    dummy((f(3), nullptr));

我想知道 dummy(...)部分是什么关于,所以我做了一些研究,但找不到任何东西。在答案提供的代码段中,使用了< utility> 标头,因此我想必须在其中的某处声明该内容,但是我仍然找不到任何内容

I wondered what the dummy(...) part was about so I did some research but couldn't find anything about it. In the code snippet provided in the answer there was the <utility> header used so I guess that thing must be declared somewhere in there, but I still couldn't find anything about it.

有人可以解释 dummy 函数(或函子)的作用,声明位置以及通常使用的功能

Could someone explain what that dummy function (or functor) does, where it is declared and what it is usually used for?

我的意思显然是在示例中用于调用函数f。但是它的实际目的是什么?

I mean obviously in the example it is used to call the function f. But what its actual purpose?

注意:我知道这个问题有点广泛,但是由于我找不到关于它的任何信息,因此无法将问题集中在一个具体问题上。另外,我希望我的问题的答案能帮助其他人找到有关神秘的 dummy()的信息。

NOTE: I know that question is a little broad, but since I couldn't find any information about it I could not focus the question onto one specif problem. Also I hope that an answer to my questions will help others finding information about the mysterious dummy().

推荐答案

让我们使用更简单的类型和表达式来简化声明。我们将使用 int 代替 std :: function< void(int)> 42 代替lambda,而 f + = 1 代替 f(3)

Let's simplify the declaration a bit by using simpler types and expressions. We'll use int instead of std::function<void(int)>, 42 instead of the lambda, and f += 1 instead of f(3):

int f{42}, dummy((f += 1, 0));

要使其更加明显,我们还可以在第二个初始化中使用花括号代替括号:

To make it even more obvious, we can also use braces instead of parentheses for the second initialisation:

int f{42}, dummy{(f += 1, 0)};

这样,它应该更清晰。这是一个声明,声明了两个变量: f dummy f 初始化为 42 ,而 dummy 初始化为此表达式:(f + = 1,0)。那个使用逗号运算符首先计算 f + = 1 ,丢弃结果,然后使用值 0 初始化 dummy

This way, it should be clearer. It's a declaration which declares two variables: f and dummy. f is initialised with 42, and dummy is initialised with this expression: (f += 1, 0). That one's using the comma operator to first evaluate f += 1, discard the result, and then use the value 0 to initalise dummy.

返回完整(非简化)声明:

Going back to the full (nonsimplified) declaration:

变量 f dummy 的类型均为 std: :function< void(int)> 。第一个 f 用一个lambda初始化。然后,使用逗号表达式初始化 dummy 。该表达式的左侧 f(3)被求值并忘记了。右边的 nullptr 用于初始化 dummy 。用 nullptr 初始化 std :: function 会导致创建一个空的 std :: function 对象(与默认构造的对象相同)。

The type of both variables f and dummy is std::function<void(int)>. First f is initialised with a lambda. Then, dummy is initialised using a comma expression. The left-hand side of that expression, f(3), is evaluated and forgotten. The right-hand side, nullptr, is then used to initialise dummy. Initialising a std::function with nullptr results in creating an empty std::function object (the same as a default-constructed one).

dummy 是在同一行(=同一声明中)引入一些额外的上下文,在这些上下文中可以调用 f

The whole purpose of dummy is to introduce some extra context on the same line (= in the same declaration) in which f could be invoked.

这篇关于dummy()函数-应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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