首页 > 代码库 > Local Functions

Local Functions

int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function.
    return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)

BOOST_TEST(add(1, 2) == 3); // Local function call.


int BOOST_LOCAL_FUNCTION(void) { // No parameter.
    return 10;
} BOOST_LOCAL_FUNCTION_NAME(ten)

BOOST_TEST(ten() == 10);

This library introduces the new "keyword" bind [9] which is used in place of the parameter type to specify the name of a variable in scope to bind (therefore, bind cannot be used as a local function parameter type). A variable can be bound by value:

bind variable-name // Bind by value.
Or by reference prefixing the variable name with &:

bind& variable-name // Bind by reference.
Furthermore, the "keyword" bind can be prefixed by const to bind the variable by constant value:

const bind variable-name // Bind by constant value.
Or by constant reference:

const bind& variable-name // Bind by constant value.

int main(void) {                            // 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_TEST(sum == 60);                  // Assert final summation value.
    return boost::report_errors();
}

Binding the Object this

It is also possible to bind the object this when it is in scope (e.g., from an enclosing non-static member function). This is done by using the special symbol this_ (instead of this) as the name of the variable to bind in the local function declaration and also to access the object within the local function body. [13]

[Warning]Warning

The library will generate a compile-time error if this is mistakenly used instead of this_ to bind the object in the local function declaration. However, mistakenly using thisinstead of this_ to access the object within the local function body will leads to undefined behaviour and it will not necessarily generate a compile-time error. [14]Programmers are ultimately responsible to make sure that this is never used within a local function.

The object this can be bound by value:

bind this_ // Bind the object `this` by value.

In this case the local function will be able to modify the object when the enclosing scope is not a constant member and it will not be able to modify the object when the enclosing scope is a constant member. Otherwise, the object this can be bound by constant value:

const bind this_ // Bind the object `this` by constant value.

struct adder {
    adder() : sum_(0) {}

    int sum(const std::vector<int>& nums, const int factor = 10) {

        void BOOST_LOCAL_FUNCTION(const bind factor, bind this_, int num) {
            this_->sum_ += factor * num; // Use `this_` instead of `this`.
        } BOOST_LOCAL_FUNCTION_NAME(add)

        std::for_each(nums.begin(), nums.end(), add);
        return sum_;
    }

private:
    int sum_;
};