C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 16 Nov 2023 13:59:55 -0500
On Thu, Nov 16, 2023 at 1:12 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
>
>
> On Thursday, November 16, 2023, Sebastian Wittmeier via Std-Proposals wrote:
>>
>> And it is not very obvious to the caller.
>
>
>
> If I decide to use this tactic in my Arduino microcontroller code, then I'll probably do:
>
> template<typename T>
> void Func(void)
> {
> // stuff in here
> }
>
> #define CALLSITE decltype([]{})
>
> int main(void)
> {
> Func<CALLSITE>();
> }
>
> It would be nice though if this could be standardised to:
>
> template<_Callsite>
> void Func(void)
> {
> // stuff in here
> }
>
> int main(void)
> {
> Func<_Callsite>();
> }
>
> but I don't think the committee guys care.

Hacks shouldn't be standardized unless they're highly useful. And even
then, they should be properly standardized, rather than providing a
standard way to invoke the hack.

Boost::lambda provided a hack way to add lambda expressions to the
language in the pre-C++11 days. The committee did not standardize the
hack; they created a proper language feature.

> Also, in the first example I gave above, it would be nice to be able to constrain a template so that the parameter type must be a lambda, for example
>
> template<typename T> requires is_lambda_v< remove_cvref_t<T> >
> void Func(void)
> {
> // stuff in here
> }
>
> but for some reason lots of people here don't want to have an "is_lambda_v" in the standard.

That's because you should never want to do that. If a template accepts
a callable type, it should accept *any* callable type, not just a
lambda. A user should not be prevented from providing a concrete type
if they so desire, and any interface that wants to prevent them from
doing so is a *bad* interface.

Even in this case, where you're using the side-effect of a lambda
expression to hack something in, it's wrong to want to force this on
the user. The reason being that a user might simply want to get the
same instantiation of a function in some cases. They can do so by
providing a known, concrete type.

Received on 2023-11-16 19:00:08