C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Lambda type not isolated within function

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 20 Apr 2023 20:49:12 +0100
On Thu, Apr 20, 2023 at 4:05 PM Giuseppe D'Angelo via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> > auto mylambda = [arg](void) -> int
> > {
> > return arg + 3;
> > };
>>
> to
> >
> > struct {
> > int arg;
> > auto operator()() const -> int { return arg + 3; }
> > } mylambda = { arg };



This argument can be made for __every__ lambda that was ever written.
We can replace them all with structs. But lambdas are all about
neatness and convenience.

Personally I think lambda types should have a few typedefs. I think
the following lambda:

    [&argc,=argv](int &a, float b) -> bool { return (argc++ >
argv[0][0]) && (a++ < b); }

should be treated by the compiler as though it were:

struct LambdaType {

    bool operator()(float const a, char const *&p) const
    {
        return (this->captures_struct.argc++ >
this->captures_struct.argv[0][0]) && (a < *p++);
    }

    typedef bool ReturnType;
    typedef std::tuple<float const,char const *&> ParamTypes;

    struct CapturesStruct {
        int &argc;
        char **argv;
    };

    typedef std::tuple<int&,char**> CapturesTuple;

    union {
        CapturesStruct captures_struct;
        CapturesTuple captures_tuple ;
    };

    LambdaType(CapturesStruct &&arg) : captures_struct(arg) {}
    LambdaType(CapturesTuple &&arg) : captures_tuple (arg) {}

    typedef bool ( *FuncPtr)(float const,char const*&p) ;
    typedef bool (LambdaType::*MemFuncPtr)(float const,char const*&p) const;

    static consteval bool is_trivial(void)
    { return std::is_trivial_v<CapturesStruct>; }
    static consteval bool is_default_constructible(void)
    { return std::is_default_constructible_v<CapturesStruct>; }
};

Received on 2023-04-20 19:49:24