Title
Implementation constraints on reference-only closure objects
Status
cd2
Section
7.5.5.2 [expr.prim.lambda.closure]
Submitter
Daveed Vandevoorde

Created on 2008-12-10.00:00:00 last changed 171 months ago

Messages

Date: 2009-07-15.00:00:00

[Voted into the WP at the July, 2009 meeting as part of N2927.]

Date: 2009-07-15.00:00:00

Proposed resolution (July, 2009)

See document PL22.16/09-0117 = WG21 N2927. (See also document PL22.16/09-0035 = WG21 N2845, which partially addressed this issue by the removal of std::reference_clossure.)

Date: 2009-03-23.00:00:00
N2800 comment USĀ 73

Consider an example like:

    void f(vector<double> vec) {
      double x, y, z;
      fancy_algorithm(vec, [&]() { /* use x, y, and z in various ways */ });
    }

7.5.5 [expr.prim.lambda] paragraph 8 requires that the closure class for this lambda will have three reference members, and paragraph 12 requires that it be derived from std::reference_closure, implying two additional pointer members. Although 9.3.4.3 [dcl.ref] paragraph 4 allows a reference to be implemented without allocation of storage, current ABIs require that references be implemented as pointers. The practical effect of these requirements is that the closure object for this lambda expression will contain five pointers. If not for these requirements, however, it would be possible to implement the closure object as a single pointer to the stack frame, generating data accesses in the function-call operator as offsets relative to the frame pointer. The current specification is too tightly constrained.

Lawrence Crowl:

The original intent was that the reference members could be omitted from the closure object by an implementation. The problem we had was that we want the call to f in

    extern f(std::reference_closure<void()>);
    extern f(std::function<void()>);
    f([&](){});

to unambiguously bind to the reference_closure; using reference_closure can be an order of magnitude faster than using function.

(See also issue 751.)

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: wp -> cd2
2009-11-08 00:00:00adminsetstatus: dr -> wp
2009-08-03 00:00:00adminsetmessages: + msg2230
2009-03-23 00:00:00adminsetmessages: + msg2017
2009-03-23 00:00:00adminsetstatus: open -> dr
2008-12-10 00:00:00admincreate