How can I make the storage of C++ lambda objects more efficient?

The standard advice you see on the Internet is to store the lambda in a std::function object. However, none of this advice ever considers the storage implications.

That's because it doesn't matter. You do not have access to the typename of the lambda. So while you can store it in its native type with auto initially, it's not leaving that scope with that type. You can't return it as that type. You can only stick it in something else. And the only "something else" C++11 provides is std::function.

So you have a choice: temporarily hold on to it with auto, locked within that scope. Or stick it in a std::function for long-term storage.

Is all this copying really necessary?

Technically? No, it is not necessary for what std::function does.

Is there some way to coerce the compiler into generating better code?

No. This isn't your compiler's fault; that's just how this particular implementation of std::function works. It could do less copying; it shouldn't have to copy more than twice (and depending on how the compiler generates the lambda, probably only once). But it does.


I noticed the same performance problem a while ago with MSVC10 and filed a bug report at microsoft connect :
https://connect.microsoft.com/VisualStudio/feedback/details/649268/std-bind-and-std-function-generate-a-crazy-number-of-copy#details

The bug is closed as "fixed". With MSVC11 developer preview your code now indeed print :

Constructing simple!
Copying simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

Your first problem is simply that MSVC's implementation of std::function is inefficient. With g++ 4.5.1 I get:

Constructing simple!
Copying simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

That's still creating an extra copy though. The problem is that your lambda is capturing test by value, which is why you have all the copies. Try:

int main()
{
    Simple test( 5 );

    std::function<int ()> f =
        [&test] ()               // <-- Note added &
        {
            return test.Get();
        };

    printf( "%d\n", f() );
}

Again with g++, I now get:

Constructing simple!
5
Destroying simple!

Note that if you capture by reference then you have to ensure that test remains alive for the duration of f's lifetime, otherwise you'll be using a reference to a destroyed object, which provokes undefined behaviour. If f needs to outlive test then you have to use the pass by value version.

Tags:

C++

C++11