C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Callsite passed as template parameter

From: Pavel Vazharov <freakpv_at_[hidden]>
Date: Wed, 15 Nov 2023 15:52:23 +0200
On Wed, Nov 15, 2023 at 3:45 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Wed, Nov 15, 2023 at 1:39 PM Pavel Vazharov <freakpv_at_[hidden]> wrote:
> >
> > As far as I know the standard mandates that every lambda has a unique
> type.
> > I don't think that the compiler is allowed to do the "collapsing" of the
> "same" lambda objects to a single type but I can't prove it as I'm not so
> versed with the C++ standard.
>
>
> Yeah every lambda must have a unique type:
>
> int main(void)
> {
> auto lambda1 = []{};
> auto lambda2 = []{};
> static_assert( false == std::is_same_v< decltype(lambda1),
> decltype(lambda1) > );
> }
>
> I'm not saying that two lambdas might have the same type. The above
> static_assert must always succeed. What I'm saying is: When the
> compiler encounters:
>
> template<typename = decltype([]{})>
>
> Is the compiler free to have one global lambda type that is shared as
> a default template parameter among all the instantiations?
>
> I think not because then the uniqueness of the lambda type will be lost in
this case.

Excerpt from the standard
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4928.pdf>:
7.5.5.2 Closure types [expr.prim.lambda.closure]
1 The type of a lambda-expression (which is also the type of the closure
object) is a unique, unnamed non-union
class type, called the closure type, whose properties are described below.
2 The closure type is declared in the smallest block scope, class scope, or
namespace scope that contains the
corresponding lambda-expression.

Received on 2023-11-15 13:52:36