我有 8 年的编码经验,但我从未见过将运算符[]
作为参数传递给函数定义。
例如,以下代码(来自开源项目):
bree::porder(m_root, [] (treenode* node) { delete node; });
在我的编码生涯中,我一直将其定义[]
为运算符重载器,而不是参数。
那么这个新语法意味着什么?
我正在使用 Visual Studio 2003 附带的编译器。如何更改上述代码以便在 VS 2003 中编译?
我有 8 年的编码经验,但我从未见过将运算符[]
作为参数传递给函数定义。
例如,以下代码(来自开源项目):
bree::porder(m_root, [] (treenode* node) { delete node; });
在我的编码生涯中,我一直将其定义[]
为运算符重载器,而不是参数。
那么这个新语法意味着什么?
我正在使用 Visual Studio 2003 附带的编译器。如何更改上述代码以便在 VS 2003 中编译?
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.
It looks like the C++0x syntax for an anonymous function
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:
// somewhere where it would be syntactically valid to
// define a function
void treenode_deleter(treenode* node)
{
delete node;
}
// ...
bree::porder(m_root, treenode_deleter);