newmerce的个人博客分享 http://blog.sciencenet.cn/u/newmerce

博文

Understanding Lambda-Capture (理解lamda捕获含义)

已有 2949 次阅读 2012-11-18 11:17 |个人分类:C/C++|系统分类:科研笔记| border, Top, HIDDEN, frame

Symbol

Indicates

[]

Nothing to capture: an up-level reference is an error

[&x, y, ...]

Capture as specified: identifiers prefixed by & are captured by reference; other identifiers are captured by copy. An up-level reference to any variable not explicitly listed is an error

[&]

Capture by reference: an up-level reference implicitly captures the variable by reference

[=]

Capture by copy: an up-level reference implicitly captures the variable by copy

[&, x, y, ...]

Capture by reference with exceptions: listed variables are captured by value/copy (no listed variable may be prefixed by &)

[=, &x, &y, ...]

Capture by copy with exceptions: listed variables are captured by reference only (every listed variable must be prefixed by &)

以上参见Intel reference。
例子:
float radius[] = {2,3,5,7};
float area=0;
float pi = 3.14f;
for_each(radius, radius+4, [&](float r)->float {return area+=pi*r*r;}); 
按引用方式捕获area。


https://blog.sciencenet.cn/blog-259088-633650.html

上一篇:不能将参数 1 从“const char *”转换为“LPCWSTR“的解决办法
下一篇:A full example usage for tbb::blocked_range2d with comments
收藏 IP: 110.185.182.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-28 01:59

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部