【问题标题】:What's the meaning of this C++ syntax [duplicate]这个C++语法是什么意思[重复]
【发布时间】:2015-06-01 15:09:23
【问题描述】:

我找到了这个 c++ 代码,但我无法理解这个语法:

auto path_dbus = [&](DBus::Connection &bus) {
    ...
};

【问题讨论】:

    标签: c++ dbus


    【解决方案1】:

    这是一个 lambda 函数:

    • 通过引用[&]捕获任何使用的变量
    • 接受参数(DBus::Connection&)
    • 做了一些工作{...}

    分解那条线:

    auto path_dbus = [&]      (DBus::Connection &bus) {... };
                     ^capture ^arguments              ^work
    

    【讨论】:

    • “当前范围内的所有内容”具有误导性。仅捕获使用的那些。 (§5.1.2[expr.prim.lambda]/11)
    • @RJFalconer 这不是实现细节吗?
    • @eush77 有一个nicely-written explanation here,滚动到“捕获条款”(特别是“A common misconception about capture-default is that all variables in the scope are captured whether they are used in the lambda or not”)
    • @RJFalconer 抱歉,您能详细说明一下吗?此行为在 §5.1.2 中定义,因此从技术上讲不是实现细节(而且我的原始评论是错误的),但作为该语言的用户,我无法判断是捕获了所有变量还是仅捕获了那些变量,可以吗?您的链接没有解释为什么这种误解值得担心。
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2012-08-21
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多