Strange C ++ Syntax

I have 8 years of coding experience, but I have never seen that the [] operator is passed as a parameter to a 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 reloader, and not as a parameter.

So what does this new syntax mean?

I am using the compiler that comes with Visual Studio 2003. How can I modify the above code to compile in VS 2003?

+8
c ++ lambda visual-c ++ square-bracket visual-studio-2003
source share
3 answers

This is C ++ lambda , you can replace the code with a function object with the same definition. The link shows two examples using Functor and one using lambda.

+16
source share

Looks like C ++ 0x syntax anonymous function

+5
source share

As mentioned in other answers, this is the β€œnew syntax to support C ++ 0x lambas. It is not supported in any version of Visual Studio prior to VS 2010, so to get this code snippet to work in VS 2003, you will need to rewrite the code for using a function or functor object.

I think there might be something like the following 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); 
+5
source share

All Articles