Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:45:47+00:00 2026-06-13T17:45:47+00:00

I have an ObjectiveC++ project. In the ObjectiveC context I am using ARC and

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:45:48+00:00Added an answer on June 13, 2026 at 5:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project is ARC enabled (the build settings have Objective-C Reference Counting set to
I am using camel 2.9.0 in my project. We have a number of routes
I have some synchronization in my Objective-C project. The code looks like this: -
In my objective-c project, I have a protocol like this: @protocol MyProtocol -(id) get:(NSString
I have this code: - (CGImageRef)createImageWithContext:(CGContextRef)context { return CGBitmapContextCreateImage(context); } - (void)fooWithContext:(CGContextRef)context { CGImageRef
This is driving me crazy! I even have a different project with the exact
I'm new to objective C and currently working on a small project. I have
I have some Objective-C code that looks like this: #define myVar 10 float f
I have this Objective-C code that I found somewhere. Honestly, I don't understand a
Background : I have an Apple Dashcode v3 project in which there is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.