A "functional" style of programming has become common in C++, following the introduction of the "Standard Template Library" (STL) into C++'s standard library. C++ is, however, notably lacking in its support for this style and thus cannot take full advantage of its own standard libraries. C++'s mechanisms for defining functions or objects to pass to STL algorithms are overly verbose. The effective use of modern C++ libraries calls for lambda functions in the language.
This paper describes a design and implementation of built-in lambda functions for C++. C++'s compilation model, where activation records are maintained in a stack, and the lack of automatic object lifetime management, make safe lambda functions and closures challenging: if a closure outlives its scope of definition, references stored in a closure dangle. Our design is careful to balance between conciseness of syntax and explicit annotations to guarantee safety.
Lambda functions can be declared without annotating their parameter types. C++0x, the forthcoming revision of standard C++, supports constrained templates and modular type checking. We describe how to infer parameter types of lambda functions from the constraints of generic functions in order to support modular type checking in the presence of lambda functions. Our design is currently under consideration for adoption to C++0x.
[1]
Yannis Smaragdakis,et al.
Functional programming with the FC++ library
,
2004,
J. Funct. Program..
[2]
Jaakko Järvi,et al.
The Lambda Library: unnamed functions in C++
,
2003,
Softw. Pract. Exp..
[3]
Thomas M. Breuel.
Lexical Closures for C++
,
1988,
C++ Conference.
[4]
Bjarne Stroustrup,et al.
Concepts: linguistic support for generic programming in C++
,
2006,
OOPSLA '06.
[5]
Michael J. Vilot,et al.
Standard template library
,
1996
.
[6]
Jaakko Järvi,et al.
C++ Function Object Binders Made Easy
,
1999,
GCSE.
[7]
Jaakko Järvi,et al.
Variadic templates for C++
,
2007,
SAC '07.
[8]
Alexander A. Stepanov,et al.
C++ Standard Template Library
,
2000
.
[9]
Guy L. Steele,et al.
The Java Language Specification
,
1996
.
[10]
John Freeman,et al.
Lambda Expressions and Closures: Wording for Monomorphic Lambdas (Revision 3)
,
2010
.
[11]
James Gosling,et al.
The Java Language Specification, 3rd Edition
,
2005
.