Lambda functions for C++0x

  • Authors:
  • Jaakko Järvi;John Freeman

  • Affiliations:
  • Texas A&M University, College Station, Texas;Texas A&M University, College Station, Texas

  • Venue:
  • Proceedings of the 2008 ACM symposium on Applied computing
  • Year:
  • 2008

Quantified Score

Hi-index 0.00

Visualization

Abstract

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.