Meine Blogeinträge! Durchgeführte Projekte! Meine Interessen! MiCasaIsSuCasa! This is me!
MYBLOG

"The programmer, like the poet, works only slightly removed from pure thought-stuff.
He builds his castles in the air, from air, creating by exertion of the imagination.

Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.
- Frederick P. Brooks

Lambdas in C++0x! erstellt am 14.06.2009

The Visual C++ compiler of Visual Studio 2010 Beta 1 supports the upcoming C++0x standard and therefore introduces a great new language feature -> lambda expressions.  In this article I will look at the usage of lambda expressions by converting Scheme code constructs to C++.

First of all, let us start with a traditional "Hello World" example to see how these expressions are built. In figure 1 I created a lambda expression that simply puts “Hello World” onto the console.

The brackets [] are introducing the lambda expression. These brackets are also called capture clause, because it specifies whether the body of the expression accesses variables in the enclosing scope by value or by reference. In the example I do not need any variables, therefore the capture clause is empty. The same is with the empty parameter list. The arrow pointing to void indicates the return type of the expression. This construct can be left right away, because there is no return type needed. I just put it there for completion.

Now, let us look at lambda expressions used in Scheme. In the example below, I firstly define a procedure called make-adder. It is responsible for creating procedures, which have the task to add a specific value x to a given value y. As you can see in the example the procedure adder4 adds the value of 4 to the given parameter.

Make-adder consists of nested lambda expressions, whereas the inner one is able to access the stored variable x from its outer environment.  Later on, we will see in the C++ example what this environment is and where the value of the variable x is stored. But for now it is only necessary to understand what this code does.

An equivalent code example in the upcoming C++0x standard can be seen in figure 3. Besides the wired syntax, the definition of the make_adder function looks pretty similar. The same applies to the usage of make_adder.

It is also possible to convert the C++0x code to the current C++ standard. Therefore you need to know that lambda expressions can be represented by using function objects. This means we simply need to create objects which overload the function call operator () in order to get the same functionality as the lambda expressions showed above. Such objects are also called functors.

The definition of make_adder is now a bit different. With the current C++ standard it is much more to write in order to get the same functionality. As you will probably recognize when watching the code in figure 4 is that make_adder is not really a function. It is a class object which makes use of the operator (). This technique is useful because an object of the class CAdder can now be called like a function but it also has a state.

This is exactly where the environment comes into play. Recognize the temporary variable _x which is stored in the CAdder class object. As noted above a lambda expression maintains state by using an environment. Here _x represents the environment for the CAdder class object.

For a complete understanding of the above code I suggest testing these examples on your own. You will find additional information of lambda expressions in the MSDN Library. If you have any questions just email me.


2 Kommentare
 
  Kommentar hinzufügen | Permalink  


Anzeigename: deSiriX-oNe
Realname: Denis Dwornitzak
Studiengang:
Software-Engineering

Kontakt: E-Mail

Weitere Informationen gibt's hier...
 
Die letzten Einträge
 
Links
 
Terminübersicht

C# 3.0 und LINQ

Datum: 24.09.2009 - 09:00 Uhr
Dauer: ca. 4 Std.
Ort: HS Esslingen, Geb.1 Raum F1.205
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

Windows Presentation Foundation

Datum: 25.09.2009 - 09:00 Uhr
Dauer: ca. 4 Std.
Ort: HS Esslingen, Geb.1 Raum F1.205
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

The almighty .NET language - C++/CLI

Datum: 30.05.2009 - 08:30 Uhr
Dauer: ca. 4 Std.
Ort: HS Heilbronn im Raum F025
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

Windows Presentation Foundation

Datum: 02.03.2009 - 08:30 Uhr
Dauer: ca. 4 Std.
Ort: HS Heilbronn im Raum F025
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien