13.16 Function Expressions

Ru-Brd

Chapter 22 illustrates that it is often convenient to pass small functions (or functors) as parameters to other functions. We also mention in Chapter 17 that expression template techniques can be used to build small functors concisely without the overhead of explicit declarations (see Section 18.3 on page 340).

For example, we may want to call a particular member function on each element of a standard vector to initialize it:

 class BigValue {    public:      void init();   };  class Init {    public:      void operator() (BigValue& v) const {          v.init();      }  };  void compute (std::vector<BigValue>& vec)  {      std::for_each (vec.begin(), vec.end(),                     Init());   } 

The need to define a separate class Init for this purpose is unwieldy. Instead, we can imagine that we may write (unnamed) function bodies as part of an expression:

 class BigValue {    public:      void init();   };  void compute (std::vector<BigValue>& vec)  {      std::for_each (vec.begin(), vec.end(),                     $(BigValue&) { .init(); });   } 

The idea here is that we can introduce a function expression with a special token $ followed by parameter types in parentheses and a brace - enclosed body. Within such a construct, we can refer to the parameters with the special notation $n , where n is a constant indicating the number of the parameter.

This form is closely related to so-called lambda expressions (or lambda functions ) and closures in other programming languages. However, other solutions are possible. For example, a solution might use anonymous inner classes, as seen in Java:

 class BigValue {    public:      void init();   };  void compute (std::vector<BigValue>& vec)  {      std::for_each (vec.begin(), vec.end(),                     class {                       public:                         void operator() (BigValue& v) const {                             v.init();                         }                     };);   } 

Although these sorts of constructs regularly come up among language designers, concrete proposals are rare. This is probably a consequence of the fact that designing such an extension is a considerable task that amounts to much more than our examples may suggest. Among the issues to be tackled are the specification of the return type and the rules that determine what entities are available within the body of a function expression. For example, can local variables in the surrounding function be accessed? Function expressions could also conceivably be templates in which the types of the parameters would be deduced from the use of the function expression. Such an approach may make the previous example even more concise (by allowing us to omit the parameter list altogether), but it brings with it new challenges for the template argument deduction system.

It is not at all clear that C++ will ever include a concept like function expressions. However, the Lambda Library of Jaakko J rvi and Gary Powell (see [LambdaLib]) goes a long way toward providing the desired functionality, albeit at a considerable price in compiler resources.

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net