Apple block vs c++ 11 Lambdas

时间:2022-06-19 20:58:29

I"ve been playing around with C++11 and Apple blocks, and I've tried to create sort of an interator function. The code:

我一直在玩c++ 11和Apple block,我试着创建一个interator函数。代码:

#include <functional>
#include <stdio.h>

void range(int low, int high, int& ref, std::function<void(void)> block){
    int a = low;
    while(a < high){
        ref = a;
        block();
        a++;
    }
}

void range(int low, int high, int& ref, void (^block)(void)){
    int a = low;
    while(a < high){
        ref = a;
        block();
        a++;
    }
}

int main(){
    int a = 0;
    range(0, 5, a, [&](){
        printf("%i\n", a);
    });

    int b = 0;
    range(0, 5, b, ^(){
        printf("%i\n", b);
    });
}

The first one, using C++11 Lambdas worked as I expected, and gives the following output

第一个,使用c++ 11 Lambdas按照我的期望工作,并给出以下输出

0
1
2
3
4

The second one, using the Apple Blocks API, gives 5 zeroes, is there any way to make it work for blocks too?

第二个,使用Apple Blocks API,给出5个0,有没有办法让它也适用于block ?

1 个解决方案

#1


6  

Quoting from the source directly:

直接从来源引用:

Only the value is captured, unless you specify otherwise. This means that if you change the external value of the variable between the time you define the block and the time it’s invoked, the value captured by the block is unaffected.

只有值被捕获,除非您指定其他值。这意味着,如果在定义块的时间和调用它的时间之间更改变量的外部值,则块捕获的值不会受到影响。

The value captured is a copy of int b which has an initial value of 0.

捕获的值是初始值为0的int b的副本。

Then they go on to specify:

然后他们接着说:

If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration.

如果您需要能够从块中更改捕获的变量的值,您可以在原始变量声明中使用__block存储类型修饰符。

And they provide the following code sample:

并提供以下代码示例:

__block int anInteger = 42;

void (^testBlock)(void) = ^{
    NSLog(@"Integer is: %i", anInteger);
};

anInteger = 84;
testBlock(); // Prints "Integer is: 84"

I advise you to stick with Lambdas.

我建议你坚持用羔羊肉。

#1


6  

Quoting from the source directly:

直接从来源引用:

Only the value is captured, unless you specify otherwise. This means that if you change the external value of the variable between the time you define the block and the time it’s invoked, the value captured by the block is unaffected.

只有值被捕获,除非您指定其他值。这意味着,如果在定义块的时间和调用它的时间之间更改变量的外部值,则块捕获的值不会受到影响。

The value captured is a copy of int b which has an initial value of 0.

捕获的值是初始值为0的int b的副本。

Then they go on to specify:

然后他们接着说:

If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration.

如果您需要能够从块中更改捕获的变量的值,您可以在原始变量声明中使用__block存储类型修饰符。

And they provide the following code sample:

并提供以下代码示例:

__block int anInteger = 42;

void (^testBlock)(void) = ^{
    NSLog(@"Integer is: %i", anInteger);
};

anInteger = 84;
testBlock(); // Prints "Integer is: 84"

I advise you to stick with Lambdas.

我建议你坚持用羔羊肉。