Default Overhead Closing

Is there any overhead when using the default capture mode?

{ Foo foo = ...; Bar bar = ...; [&]() { write(foo); } } { Foo foo = ...; Bar bar = ...; [&foo]() { write(foo); } } 

To find out if there is any cost to using the first bar associated with the capture, even if it is not used?

+6
source share
2 answers

It is not known how lambda deals with objects captured by reference from the section of standard C ++ 14 ( N4140 ) 5.1.2 [expr.prim.lambda]:

An object is captured by reference if it is implicitly or explicitly captured, but not captured by the copy. Unknown, additional unnamed non-static data elements are declared in the closing type for objects captured by reference. A member of an anonymous connection should not be fixed by reference

This wording was intentionally left open to allow implementations to optimize around this, see bug report 750: implementation restrictions for closures only for links that are referenced N2927 , which states:

The new edition no longer indicates any elements of rewriting or closing for "by reference". The use of entities captured "by reference" affects the original entities, and the mechanism for achieving this is completely left for implementation.

+3
source

N3337 5.1.2 / 15 or N4527 (final C ++ 14) 5.1.2 / 16

An object is captured by reference if it is implicitly or explicitly captured, but not captured by the copy. Not specified, additional unnamed non-static data elements are declared in the closing type for objects captured by the link.

+2
source

All Articles