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 7066267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:03:45+00:00 2026-05-28T05:03:45+00:00

I made a finally simulator using lambda in C++11 as below: #include <cstdio> template<typename

  • 0

I made a finally simulator using lambda in C++11 as below:

#include <cstdio>

template<typename Functor>
struct Finalizer
{
    Finalizer(Functor& func) : func_(func) {} // (1)
    ~Finalizer() { func_(); }

private:
    Functor func_; // (2)
};

template<typename functor>
Finalizer<functor> finally(functor& func)
{
    return Finalizer<functor>(func); (3)
}

int main()
{
    int a = 20;

    // print the value of a at the escape of the scope
    auto finalizer = finally([&]{ printf("%d\n", a); }); // (4)
}

The code works as intended, but there is undesired copy ctor call (of lambda functor) at the ctor of Finalizer struct (1). (Thankfully, copy construction at the return statement in the finally function (3 -> 4) is avoided by RVO.)

Compiler does not eliminate the copy ctor call (at least in vc10 – gcc may optimize it), and if the type of the functor in Finalizer struct (2) is changed to reference it’ll crash since the lambda argument at the finally call (4) is r-value.

Of course the code can be “optimized” like below

template<typename Functor>
struct Finalizer
{
    Finalizer(Functor& func) : func_(func) {}
    ~Finalizer() { func_(); }

private:
    Functor& func_;
};

int main()
{
    int a = 20;

    auto finalizer = [&]{ printf("%d\n", a); };
    Finalizer<decltype(finalizer)> fin(finalizer);
}

No overhead, only a printf call is placed at the end of scope. But… I don’t like it. 🙁 I tried to wrap it with macro, but it needs to declare two “name” – one for lambda object, the other for finalizer object.

My objective is simple –

  1. Every unnecessary performance overhead which can be avoided should be eliminated. Ideally, there should be no function call, every procedure should be inlined.
  2. Keep the concise expression as its purpose of utility function. Use of macro is allowed, but discouraged.

Is there any solution to avoid it for this situation?

  • 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-05-28T05:03:46+00:00Added an answer on May 28, 2026 at 5:03 am

    I presume lambdas have move constructors? If so, and if you will only ever use rvalues inside finally, then && and forward will move rather than copy.

    #include <cstdio>
    
    template<typename Functor>
    struct Finalizer
    {
        Finalizer(Functor&& func) : func_(std::move(func)) {}
        Finalizer(Functor const& func) : func_(func) {} // (1)
        ~Finalizer() { func_(); }
    
    private:
        Functor func_; // (2)
    };
    
    template<typename functor>
    Finalizer<std::remove_reference<functor>::type> finally(functor&& func)
    {
        return Finalizer<std::remove_reference<functor>::type>(std::forward<functor>(func)); // (3)
    }
    
    int main()
    {
        int a = 20;
    
        // print the value of a at the escape of the scope
        auto finalizer = finally([&]{ printf("%d\n", a); }); // (4)
    }
    

    It should be possible to right something more intelligent that will work correctly with lvalues too, so that you’re ‘optimized’ version will compile and will copy when it cannot move. In that case, I suggest you use something like Functor<std::remove_reference<functor>::type> to be sure that the Functor is of the right type, regardless of whether the parameters were passed around by & or && or whatever.

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

Sidebar

Related Questions

Finally I made a game using Visual C# ... (With Help Of Stackoverflow members
I finally made it to upload a file using core java. Here is my
This morning I finally made my mind and decided to ask you for help.
String a=(Yeahhhh) I have finally made it to the (top); Given above String, there
As you can guess from the title, the outlining with regions finally made me
I finally made a great step by abandoning SVN for Git and loving it.
So, I've finally made the plunge, and have gotten to the state where I'm
I made my own small thread-safe (confirmed it using many tests)library similar to Looper
I have a project made using Java. I have a complex processing, something like
I've just started using filestream, and although I made the code work,- I would

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.