Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange C++ syntax

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?

like image 313
Arjun Avatar asked Jul 24 '11 05:07

Arjun


People also ask

What is the syntax of C?

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction.

Why is C++ syntax so weird?

One of the reasons C++ is rather complicated is because it was designed to address problems that crop up in large programs. At the time C++ was created as AT&T, their biggest C program was about 10 million lines of code. At that scale, C doesn't function very well.


1 Answers

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.

like image 156
rerun Avatar answered Sep 23 '22 03:09

rerun