问一下Lambda表达式

karorokiki 2011-11-19 05:17:41
Lambda表达式 实际是类么 和函数对象 一样么
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
FrankHB1989 2011-11-20
  • 打赏
  • 举报
回复
是。
ISO C++11
5.1.2
2 The evaluation of a lambda-expression results in a prvalue temporary (12.2). This temporary is called the closure object. A lambda-expression shall not appear in an unevaluated operand (Clause 5). [ Note: A closure object behaves like a function object (20.8).—end note ]
3 The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression. [ Note: This determines the set of namespaces and classes associated with the closure type (3.4.2). The parameter types of a lambda-declarator do not affect these associated namespaces and classes. —end note ] An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:
— the size and/or alignment of the closure type,
— whether the closure type is trivially copyable (Clause 9),
— whether the closure type is a standard-layout class (Clause 9), or
— whether the closure type is a POD class (Clause 9).
sanae 2011-11-20
  • 打赏
  • 举报
回复
template <typename T>
void foo(T bar){
bar();
}

auto x = [](){...};
foo(x);

gcc4.6.1中 实例化的T和推断auto都是一个类,它包含了刚才写的lambda作为operator (),
lambda也就是operator ()是(非静态)成员函数,this指针似乎与capture的内容有关……
iamnobody 2011-11-19
  • 打赏
  • 举报
回复
匿名的函数对象,楼上正解
taodm 2011-11-19
  • 打赏
  • 举报
回复
楼主散分结束,可以结贴了。
a707000646 2011-11-19
  • 打赏
  • 举报
回复
http://baike.baidu.com/view/3580897.htm
healer_kx 2011-11-19
  • 打赏
  • 举报
回复
我还没好好研究呢,我感觉可以理解为一个函数对象?

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧