You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
A Cpp2 function expression cannot capture a reference to a callable passed via a forwarding reference (and then call it), meaning that the callable has to be copy constructed instead.
bluetarpmedia
changed the title
[BUG] Function expression (lambda) cannot capture a reference to a callable passed via forwarding reference
[BUG] Function expression (lambda) cannot capture (and call) a reference to a callable passed via forwarding reference
Jun 5, 2024
Describe the bug
A Cpp2 function expression cannot capture a reference to a callable passed via a forwarding reference (and then call it), meaning that the callable has to be copy constructed instead.
To Reproduce
Run cppfront on this code:
Repro on Godbolt
The program output shows the undesired copy of
is_even
:The
find_match_cpp1
C++ function demonstrates the desired behaviour. No copy ofpred
should occur.But in
find_match_cpp2
the capture ofpred
causes a copy:This Cpp2 code to capture
pred
by reference:results in this C++ capture:
[_0 = (&CPP2_FORWARD(pred))]
but the call to
_0
fails since it's a pointer that needs to be dereferenced.This Cpp2 code to capture
pred
by reference and then dereference it:results in the same C++ capture as above (
_0 = (&CPP2_FORWARD(pred)
) but lowers to the following:return _0 * (x);
which results in a C++ compiler error.
This Cpp2 code succeeds:
but is less friendly to write (IMO) than the original C++.
Additional context
I was translating the
ranges::adjacent_find
code from cppreference:The text was updated successfully, but these errors were encountered: