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

  • SEARCH
  • Home
  • 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 7826873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:36:32+00:00 2026-06-02T09:36:32+00:00

I am learning the new features in c++11 and came across this problem. I’d

  • 0

I am learning the new features in c++11 and came across this problem. I’d like to capture an unique_ptr by moving it inside a lambda as an argument for for_each.

set up:

std::array<int,4> arr = {1,3,5,6};
std::unique_ptr<int> p(new int);  (*p) = 3;

attempt 1 – doesn’t work because unique_ptr doesn’t have a copy constructor. c++0x doesn’t specify the pass by move syntax.

std::for_each(arr.begin(), arr.end(), [p](int& i) { i+=*p; });

attempt 2 – use bind to bind a moved copy of p to a function that takes int&:

std::for_each(arr.begin(), arr.end(),
     std::bind([](const unique_ptr<int>& p, int& i){
          i += (*p);
     }, std::move(p))
);

Compiler complains that 'result' : symbol is neither a class template nor a function template.

The main purpose of the exercise is to understand how a movable variable be captured in a lambda that’s cached for later use.

  • 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-02T09:36:33+00:00Added an answer on June 2, 2026 at 9:36 am

    Update: you can capture a movable variable in a lambda from C++14 onwards.

    std::for_each(arr.begin(), arr.end(), [p=std::move(p)](int& i) { i+=*p; });
    

    You cannot capture a movable variable into a lambda in any straightforward way in C++11.

    Lambdas capture by copy or by reference. Thus, to capture a move-only variable, you have to wrap it in an object where copying => moving (such as std::auto_ptr). This is a nasty hack.

    In your example, you can just capture by reference, but if this was just simplified code it may not do what you wanted with the real code:

    std::for_each(arr.begin(), arr.end(), [&p](int& i) { i+=*p; });
    

    Here’s a copy-move-only wrapper:

    template<typename T>
    struct move_on_copy_wrapper
    {
        mutable T value;
    
        move_on_copy_wrapper(T&& t):
            value(std::move(t))
        {}
    
        move_on_copy_wrapper(move_on_copy_wrapper const& other):
            value(std::move(other.value))
        {}
    
        move_on_copy_wrapper(move_on_copy_wrapper&& other):
            value(std::move(other.value))
        {}
    
        move_on_copy_wrapper& operator=(move_on_copy_wrapper const& other)
        {
            value=std::move(other.value);
            return *this;
        }
    
        move_on_copy_wrapper& operator=(move_on_copy_wrapper&& other)
        {
            value=std::move(other.value);
            return *this;
        }
    
    };
    

    You can then use it like this:

    int main()
    {
        std::unique_ptr<int> p(new int(3));
        move_on_copy_wrapper<std::unique_ptr<int>> mp(std::move(p));
    
        [mp]()
        {
            std::cout<<"*mp.value="<<*mp.value<<std::endl;
        }
        ();
    
        std::cout<<"p="<<p.get()<<", mp="<<mp.value.get()<<std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Ruby and I like playing with irb to discover new features and
I came across this comparison chart that suggests that FMS costs about 4.5 grands
I've been using the CodeIgniter framework, however after learning Java and awesome features like
I would appreciate some suggestions about useful sources for learning the new features of
What is the recommended resource(s) for learning the new features in C++11? Is there
I am a .net web developer. I like learning new languages and try to
When learning new languages such as C++ from PHP, does reading other language snippets
While I'm learning a new language, I'll typically put lots of silly println's to
How does Udacity.com (new learning website) create presentation where the text is in front
I on the doorstep of a new learning curve . I want to make

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.