mardi 23 décembre 2014

Apple Blocks vs C++11 Lambdas

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



#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



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?


Aucun commentaire:

Enregistrer un commentaire