C++ Logo

std-proposals

Advanced search

Re: Idea of Lambda Classes

From: Scott Michaud <scott_at_[hidden]>
Date: Tue, 12 Jan 2021 00:18:07 -0500
Andrew,

Wouldn't this be equivalent to a local, unnamed class?

Godbolt: https://godbolt.org/z/ezn8va


On 1/11/2021 11:55 PM, Andrew Tomazos via Std-Proposals wrote:
> I have an idea for a language feature we'll call Lambda Classes.
>
> As placeholder syntax let's say like:
>
> lambda-class:
> lambda-introducer : base-specifier-list_opt {
> member-specification_opt }
>
> So for example:
>
> auto my_lambda_class = [cap1,cap2]: {
> void f() { /*...*/; }
> void g() { /*...*/; }
> };
>
> A lambda class is to a normal class object as a lambda function is to
> a normal function.
>
> A lambda class is an expression that is introduced with the usual
> capture sequence, but is followed by a colon, a (possibly empty) base
> class list, and then a class definition body.
>
> It constructs a new anonymous class type in the same fashion as a
> normal lambda, capturing any variables as specified in the
> lambda-introducer, derives the new class type from any bases in the
> base-specifier-list, and adds any members given in the
> member-specification, and the value of the expression is the
> (singular) object of that new class type.
>
> The motivation is that there are many use cases where an API asks for
> an object of a class type that implements some interface - either
> implementing a (virtual) polymorphic interface (run-time polymorphism)
> in traditional OOP, or that models a certain concept (compile-time
> polymorphism) in template meta-programming - and in many of both of
> those use case families, the response is to create a single-use class
> is created just to adapt to that API. Like lambda functions, lambda
> classes help remove a lot of the boilerplate and indirection in such
> use cases.
>
> Let me know if there is any enthusiasm for this and I'll write up a
> proper draft.
>
>

Received on 2021-01-11 23:18:09