Local Functions

35 篇文章 0 订阅
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();
}

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_;
};



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值