语法帮助:方括号括起来的函数指针 [英] Syntax help: Function pointer surrounded by square brackets

查看:120
本文介绍了语法帮助:方括号括起来的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下C ++ 11代码段中[funcParam]周围的方括号的使用,我感到困惑:

I'm confused about the use of square brackets around [funcParam] in the C++11 code segment below:

typedef std::function<std::vector<OtherType> ()> FuncPtr;

inline void create(FuncPtr funcParam)
{
    auto create_evaluator = [funcParam] ()
    {
        return anotherFunction(funcParam());
    };
    // ...
}

此行调用其上方的代码(经过简化以使其易于阅读):

The code above it called by this line (somewhat simplified to keep it readable):

create( [] () { return CONSTANT; } );

有人可以解释在两种情况下使用方括号吗?在调用代码中,它看起来像是用来创建没有名称的函数的.在第一阶段中,这是做什么的?谢谢!

Can someone explain the use of brackets in both situations? In the calling code, it looks to be used to create a function without a name. What's it doing in the first segment? Thanks!

推荐答案

create_evaluator lambda表达式(请查看该页面底部的示例).

create_evaluator is a lambda expression (check out the examples at the bottom of that page).

对于

auto create_evaluator = [funcParam] ()
{
    return anotherFunction(funcParam());
};

  • [funcParam] 是本地范围内的变量,由lambda函数捕获,因此在其他地方调用时可以在lambda函数中引用.

    • [funcParam] is a variable in the local scope that is captured by the lambda function and therefore available to be reference within the lambda function when it is called elsewhere.

      ()表示lambda函数的参数(在这种情况下为none)

      () denotes the arguments to the lambda function (none in this case)

      {return anotherFunction(funcParam());} 是lambda函数的主体.

      { return anotherFunction(funcParam()); } is the body of the lambda function.

      通过电话

      create( [] () { return CONSTANT; } );
      

      使用另一个lambda表达式作为参数调用

      create .那个lambda参数

      create is called with another lambda expression as its argument. That lambda argument

      • 不捕获任何内容: []
      • 没有参数:()
      • 具有正文: {return CONSTANT;}

      这篇关于语法帮助:方括号括起来的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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