Functional sugar in C++0x

November 10, 2008

I found this easy to follow lamda tutorial when checking out the wikipage for C++0x. Its written by a member of the VC10 Visual Studio C++ team.

Check the tutorial out at: http://blogs.msdn.com/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx

I think lamdas are great, as it will automate much of the cumbersome task of defining functors. Functors are central to STL, but I find the amount of boilerplate code that has to be written (even for the most basic functor) to be cumbersome to write and distracting to read.

More Importantly though is that the declaration and definition of the functor is often visually removed (and logically hidden) from the local context where its used. This has made defining functors unpractical for simple one-liner tasks. In the most simple cases one can always compose the standard functor adapters and binders to do the task at hand. But for slightly less simplistic logic the code quickly becomes unreadable.  

So for logic that are is not simple enough for adapters and binders, but not complicated enough to warrant being encapsulated into its own named class.. well for such logic lamda functors is now a much better fit. I think lamda function will probably make the functor adapters obsolete, and full blown encapsulated functor class declarations even rarer. You can even store the unnamed lambda functors in a named wrapper. Lamdas might just sound like syntactic sugar, but the fact that it makes functor so much more useable will make it easier to code in a more functional manner.

Another nicefeature exemplified in the post, is the new auto keyword. In C++0x the compiler will let you use the the keyword auto in-place of a type name when the compiler already can deduce the type. Again this is just syntactic sugar, but i think it will make a big difference in the way people code. I think it will help a programmer weaken the dependency and delay the commitment to a specific type until compile time (as opposed to design time). One might not even care or know what the actual name of the type one are using is. That again will make it easier to program in a more functional style.

Maybe this is more than syntactic sugar, maybe it will fundamentally change how people think about C++ design.

Anyways that’s my two cent. Check out the post to find out the details.

Cheers

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: