奇怪的C++句法
我有 8 年的编码经验,但我从未见过将运算符 []
作为参数传递给函数定义。
例如,以下代码(来自开源项目):
bree::porder(m_root, [] (treenode* node) { delete node; });
在我的编码生涯中,我始终将 []
定义为运算符重载器,而不是参数。
那么这个新语法意味着什么呢?
我正在使用Visual Studio 2003附带的编译器。如何更改上面的代码,以便它可以在VS 2003中编译?
I have 8 years of coding experience, but I have never seen the operator []
passed as a parameter to the function definition.
For example, the following code (from an open source project):
bree::porder(m_root, [] (treenode* node) { delete node; });
Throughout my coding life, I have always defined []
as an operator overloader, not as a parameter.
So what does this new syntax signify?
I am using the compiler that comes with Visual Studio 2003. How can I change the above code so that it will compile in VS 2003?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个 C++ lambda 您可以将代码替换为函数对象相同的定义。该链接显示了两个示例,一个使用 Functor,另一个使用 lambda。
That is a c++ lambda you could replace the code with a function object of the same definition. The link shows two examples one using Functor and one using a lambda.
它看起来像 C++0x 语法href="http://www2.research.att.com/~bs/C++0xFAQ.html#lambda" rel="nofollow">匿名函数
It looks like the C++0x syntax for an anonymous function
正如其他答案所提到的,它是支持 C++0xambas 的全新语法。 VS 2010 之前的任何 Visual Studio 版本都不支持它,因此要使该代码片段在 VS 2003 中工作,您需要重新调整代码以使用函数或函子对象。
我认为以下内容可能对您有用:
As other answers have mentioned, its' a brand new syntax to support C++0x lambas. It is not supported in any version of Visual Studio prior to VS 2010, so to get that code snippet to work in VS 2003, you'll need to rejigger the code to use a function or functor object.
I think that something like the following might work for you: