Open bluetarpmedia opened 3 weeks ago
Also, just a note regarding the some_of
function from the cppreference example.
My Cpp2 translation here:
some_of: (forward r, forward pred) -> bool == {
return
ranges::cend(r) !=
ranges::adjacent_find(r, :(x, y) pred$(x) != pred$(y));
}
lowers to:
constexpr auto some_of(auto&& r, auto&& pred) -> bool
{
return
ranges::cend(r) !=
ranges::adjacent_find(r, [_0 = pred, _1 = CPP2_FORWARD(pred)](auto const& x, auto const& y) mutable -> auto { return _0(x) != _1(y); });
}
So pred
is copied twice. Should cppfront detect and avoid duplicating the captures?
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:
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: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: