Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++11 lambda functions in ARC ObjectiveC++ - how to do it properly?

I have an ObjectiveC++ project. In the ObjectiveC context I am using ARC and iPhoneSDK 6. In C++ I am using a C++11 compiler.

Lambda functions in C++11 are capturing variables with references. This concept is not really supported by ObjectiveC and by "try and error" I came up with the following solution. Are there any pitfalls I am not aware of?

Is there a better solution to this problem?

typedef std::function<void ()> MyLambdaType;

...
// m_myView will not go away. ARC managed.
UIView * __strong m_myView;

...
// In Objective C context I create a lambda function that calls my Objective C object
UIView &myViewReference = *m_myView;
MyLambdaType myLambda = [&myViewReference]() {
    UIView *myViewBlockScope = &myViewReference;
    // Do something with `myViewBlockScope`
}

..
// In C++11 context I call this lambda function
myLambda();
like image 924
Lars Schneider Avatar asked Oct 26 '12 07:10

Lars Schneider


1 Answers

The straightforward thing to do would be to let the lambda capture the object pointer variable m_myView (I am assuming from your snippet that this is a local variable), and use it normally inside the lambda:

MyLambdaType myLambda = [m_myView]() {
    // Do something with `m_myView`
}

The only concern would be the memory management of m_myView. To be generally correct, the lambda needs to retain m_myView when it is created, and release it when it is destroyed (just like blocks do; because the lambda could be used in a scope where m_myView does not exist).

Reading through the ARC docs, I don't see this situation mentioned specifically, but I believe that it should handle it properly, because (1) captured variables of a C++11 lambda are stored as fields of an anonymous class, which are initialized to the captured value when the lambda is constructed, and (2) ARC properly handles the retaining and releasing of Objective-C object fields of C++ classes on construction and destruction. Unless it says something specifically about lambdas to the contrary, or there's a compiler bug, I see no reason why it should not work.

like image 98
newacct Avatar answered Sep 25 '22 02:09

newacct