C++0x: Lambda functions and expressions

From wiki.dwarfstd.org
Jump to navigation Jump to search

For detail description of the feature, please refer to:




Lambda expression or lambda function is an expression that specifies an anonymous function object. The main motivation is to make some C++ Standard Library algorithms more usable by providing a way of passing code as arguments without having to define a separately named function.

For example, to find a person whose height is greater than 170cm, using find_if without lambda function:

class TallerThan {
  int height;
  TallerThan (int h) : height(h) { }
  bool operator() (const Person& p) { return p.Height() > height; }
find_if(people.begin(), people.end(), TallerThan(170));

With lambda function:

find_if(people.begin(), people.end(), 
        [] (const Person& p) { return p.Height() > 170; });

Proposed change to DWARF

none. For example, the compiler may create an class and a member function to represent the lambda function, and the class debugging information entry will reflect this with an DW_AT_artificial attribute.


 [] (const Person& p) { return p.Height() > 170; }
maybe implemented as:

 class __comp_gen {
   bool operator() (const Person& p) { return p.Height() > 170; }

<1><  aaa>      DW_TAG_class_type           
                DW_AT_name                  __comp_gen
                DW_AT_artificial            yes
<2><  bbb>      DW_TAG_subprogram
                DW_AT_type                  <xxx>       // bool
                DW_AT_name                  operator()
                DW_AT_external              yes
                DW_AT_low_pc                0xAAA
                DW_AT_high_pc               0xBBB
<3><  ccc>      DW_TAG_formal_parameter
                DW_AT_name                  this
                DW_AT_artificial            yes
<3><  ddd>      DW_TAG_formal_parameter     
                DW_AT_name                  p