c++0x的新特性:lambda表达式

来源:互联网 发布:软件实施工程师要求 编辑:程序博客网 时间:2024/06/15 04:30

(本文摘自c++论坛里自己帖子的回复。

GCC4.5引入这个特性。有兴趣的朋友可以编译测试下面这段代码:

#include <algorithm>#include <cmath>void abssort(float *x, unsigned N) {  std::sort(x, x+N,            [](float a, float b) {  // 注意此处的方括弧              return std::abs(a) < std::abs(b);            });}

更多参考:
1)http://gcc.gnu.org/gcc-4.5/cxx0x_status.html
2)http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2927.pdf
3)http://www.ibm.com/developerworks/cn/aix/library/au-gcc/

注意:需要添加编译器选项:-std=c++0x 或 -std=gnu++0x
Qt项目可使用:QMAKE_CXXFLAGS += -std=c++0x


【编辑】VC10对c++0x的支持:

C++0x Core Language Features In VC10: The Table
http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx
一些测试例子:
http://blogs.msdn.com/b/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx
http://www.kuqin.com/language/20090615/56815.html

原创粉丝点击