C++ Logo

std-discussion

Advanced search

Re: Guarantees over addresses from function pointers created from lambda

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Mon, 28 Apr 2025 13:16:25 +0300
On 28 Apr 2025 08:56, Tiago Freire via Std-Discussion wrote:
> I’m not convinced by this example. I have written many callback systems
> and never has “uniqueness” been relevant.
>
> Is that a good design? Or one that can’t be improved upon?
>
> Had C++ not provided this guarantee all along, would the algorithm (in
> the context of its intended end use) be undesignable?
>
> As mentioned, the only reason it would make a difference is because you
> are trying to do logic related to the function beyond “it exists, and it
> is callable”, and whatever that is there’s probably a better solution to
> be found elsewhere.
>
> In this case, what you want is a unique token and absent a feature for
> that you used an unrelated “this function address is unique” to achieve
> that.
>
> I’m not saying that maybe it isn’t necessary in this case, I don’t see
> the concrete example you might have in mind.
>
> But if the address being unique is important for this function, wouldn’t
> it be better to explicit request it with a tag [[unique_address]] and
> not make it the default everywhere?

Function addresses can be used as template arguments, and in those
instances you definitely want the standard behavior. It would be odd to
require one behavior at compile time and a different one in run time.

The history of compilers not following the standard in this area is
unfortunate, but I think the standard being the way it is is still the
right thing. Compiler deviations, while being useful and acceptable in
some respects, are still bugs (or maybe intentional deviations, if you
will). Users just need to be aware of them.

Received on 2025-04-28 10:16:28