MSPLS Spring '95 Workshop
Abstract

A Framework for Higher-Order Functions in C++

Konstantin Läufer
Dept. of Mathematical and Computer Sciences, Loyola University of Chicago

C and C++ allow passing functions as an argument to other functions in the form of function pointers. However, since function pointers can refer only to existing functions declared at global or file scope, the resulting function arguments cannot capture local environments. This leads to the common misconception that C and C++ do not support function closures.

On the contrary, function closures can be modeled directly in C++ by enclosing a function inside an object such that the local environment is captured by data members of the object. This idiom is described in advanced C++ texts and is used, for example, to implement callbacks.

The purpose of this paper is to demonstrate how this idiom can be generalized to a type-safe framework of higher-order functions that support composition and partial application. We then show informally that there is a compositional translation from functional programs to the framework. Finally, we incorporate in the framework an existing mechanism to convert member functions back to nonmember functions.

We demonstrate by means of various examples that object-oriented and functional idioms can coexist productively and can be used to enhance the functionality of common classes, for example, of nonlinear collections such as trees. A C++ implementation of the framework is available on request.


Gerald Baumgartner