Boost C++ Libraries Home Libraries People FAQ More

Next

Chapter 1. Boost.LocalFunction 1.0.0

Lorenzo Caminiti

Distributed under the Boost Software License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt)

Table of Contents

Introduction
Getting Started
This Documentation
Compilers and Platforms
Installation
Configuration
Tutorial
Local Functions
Binding Variables
Binding the Object this
Templates
Advanced Topics
Default Parameters
Commas and Symbols in Macros
Assigning and Returning
Nesting
Accessing Types (concepts, etc)
Specifying Types
Inlining
Recursion
Overloading
Exception Specifications
Storage Classifiers (auto and register)
Limitations (operators, etc)
Examples
GCC Lambdas (without C++11)
Constant Blocks
Scope Exits
Boost.Phoenix Functions
Closures
GCC Nested Functions
N-Papers
Reference
Header <boost/local_function.hpp>
Header <boost/local_function/config.hpp>
Annex: Alternatives
Annex: No Variadic Macros
Annex: Implementation
Release Notes
Bibliography
Acknowledgments

This library allows to program functions locally, directly within the scope where they are needed.

Local functions are a form of information hiding and they are useful for dividing procedural tasks into subtasks which are only meaningful locally, avoiding cluttering other parts of the program with functions, variables, etc unrelated to those parts. Local functions therefore complement other structuring possibilities such as namespaces and classes. Local functions are a feature of many programming languages, notably Pascal and Ada, yet lacking from C++03 (see also [N2511]).

Using C++11 lambda functions, it is possible to implement local functions by naming the lambdas assigning them to local variables. For example (see also add_lambda.cpp):

{                                           // Some local scope.
    int sum = 0, factor = 10;               // Variables in scope to bind.

    auto add = [factor, &sum](int num) {    // C++11 only.
        sum += factor * num;
    };

    add(1);                                 // Call the lambda.
    int nums[] = {2, 3};
    std::for_each(nums, nums + 2, add);     // Pass it to an algorithm.

    BOOST_CHECK( sum == 60 );               // Assert final summation value.
}

This library allows to program local functions portably between C++03 and C++11 (and with performances comparable to lambdas on C++11 compilers, see the Alternatives section). For example (see also add.cpp):

{                                           // Some local scope.
    int sum = 0, factor = 10;               // Variables in scope to bind.

    void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
        sum += factor * num;
    } BOOST_LOCAL_FUNCTION_NAME(add)

    add(1);                                 // Call the local function.
    int nums[] = {2, 3};
    std::for_each(nums, nums + 2, add);     // Pass it to an algorithm.

    BOOST_CHECK( sum == 60 );               // Assert final summation value.
}

This library supports the following features for local functions:

  • Local functions can capture, or better bind, any of the variables from the enclosing scope (a function together with its captured variables is also called a closure).
  • The local function body is programmed using the usual C++ statement syntax (as a consequence, compiler errors and debugging retain their usual meaning and format).
  • Local functions can be passed as template parameters so they can be conveniently used with STL algorithms, etc. [1]
  • However, local functions must be specified within a declarative context (e.g., at a point in the code where local variables can be declared) thus they cannot be specified within expressions. [2]

See the Alternatives section for a comparison between this library, C++11 lambda functions, Boost.Phoenix, and other C++ techniques that implement features related to local functions.



[1] This is a strength with respect to C++03 functors implemented using local classes which cannot be passed as template parameters (see [N2657] and the Alternatives section).

[2] This is a weakness with respect to C++11 lambda functions which can instead be specified also within expressions (see the Alternatives section).

Last revised: February 17, 2012 at 22:11:08 GMT


Next