On Thu, Apr 20, 2023 at 10:54 AM Ville Voutilainen wrote:
On Thu, 20 Apr 2023 at 17:35, Frederick Virchanza Gotham wrote:
> How about allowing the creation of new lambda object (with captures)
> by using initialiser braces?

`{3}` resembles a sequence of integers, not a callable. If this were to exist at all, it would be an explicit constructor:
    using LambdaType = decltype([arg=0]() { return arg + 3; });
    auto mylambda = LambdaType(3);
    assert(mylambda() == 6);

Why would we? If you go through all that trouble, just write a
function object class. The syntax you suggest doesn't
work - the lambda in Func is a lambda that captures the argument of
Func, to create this new kind of lambda you
would need something else,

@Ville, I agree 100% with the "Why would we?" part, but I don't see what you mean about "new kind of lambda." AIUI, there is nothing physically preventing us from giving a captureful lambda a non-explicit constructor from the types of its captures, just like we have in C++20 given a captureless lambda a non-explicit constructor from the types of its captures (i.e., a zero-argument constructor, because it has zero captures).  What keeps WG21 from adding that (mis)feature is its lack of desirability, not implementability.

The biggest usability issue is that either you'd have to permit
    int x = 1, y = 2;
    auto lam = [=]() { return x + y; };
    decltype(lam) copy = decltype(lam)(3, 4);
or disallow it.
If you disallow it, then `[=]` means something very different from `[x,y]`. Again, that's not physically problematic, it's just kinda weird from the naïve programmer's POV.
If you allow it, then either you have to specify the order of the constructor arguments (does `3, 4` initialize `x, y` or `y, x`?), which is ugly; or leave it up to the implementation, which is a portability trap.

That's enough problems that it's clear why we don't give captureful lambdas any non-special-member-function constructors. But there's nothing physically preventing such a feature. A bold enough compiler could even make every lambda type an aggregate, if it wanted to. There's nothing physically difficult about that.