C++ Logo

std-discussion

Advanced search

Re: Guarantees over addresses from function pointers created from lambda

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Sun, 27 Apr 2025 08:50:50 +0000
> Because different things should be different.
Why?
and
Are they really different?

I understand different values being different, it has arithmetic consequences.
I understand different objects being different, changing one object shouldn't change the other (need to preserve state) 🐍👀.

You are more than willing to accept that constant string/array literals defined in different places could have the same address.
The reason being if you only read from them, and the data is the same, does it really matter which copy of the data you are looking at?

If 2 functions produce the exact same byte code, does it matter which one you call?
I would even go one step further, after the code is compiled, and if you know the addresses of those functions and all the locations where they are called from, and if you replaced all the references of foo with bar and completely erased foo, would it even produce any visible side effects?
Are there any tests by which you can choose the inputs and observe the outputs, that would allow you to identify which version of the application you are running?

Now it is possible. I acknowledge that, one can do some function pointer shenanigans, where you would take the address of a function and use it to track state in a way that doesn't necessarily involve calling the function itself.
But in my entire career, the amount of times I have seen that pattern is exactly 0.
And I question the validity of such a pattern given that you are also willing to accept that the same function could yield different addresses depending of context.

so why is it again that they need to be different?


________________________________
From: Std-Discussion <std-discussion-bounces_at_[hidden]> on behalf of Jason McKesson via Std-Discussion <std-discussion_at_[hidden]>
Sent: Sunday, April 27, 2025 12:39:30 AM
To: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
Cc: Jason McKesson <jmckesson_at_[hidden]>
Subject: Re: [std-discussion] Guarantees over addresses from function pointers created from lambda

On Sat, Apr 26, 2025 at 4:06 PM Tiago Freire via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> I personally see no reason why "no such guarantees" shouldn't be part of the standard.
> I see no reason as to why comparing function addresses should yield predictable results, and saving spaces seems to me like an overwhelming compelling case as why 2 functions could have the exact same address.

Because different things should be different. If `foo` and `bar` do
not name the same entity, then they are not the same. If pointers to
functions point to a specific function entity, then pointers to those
two different entities should have different addresses because they
refer to two different entities.

We actually have an explicit rule for objects: [intro.object]/10.
Outside of nesting, zero-sized subobjects, or string/array literals,
addresses for different objects must be *different*.

To be fair, within that rule is the exception for string/array
literals. And maybe functions could be treated like literals, as
"non-unique". But it seems to me that it's reasonable to expect
compilers to not make different things behave differently. If you
never get the address of both `foo` and `bar`, then the compiler ought
to be able to play whatever games it wants. But if you explicitly get
the address of both, then the compiler needs to manifest them with
different addresses.

Granted, that might be a linker nightmare since in one TU you might
get the address of `foo`, and in another TU, you get the address of
`bar`, so you don't know that they need to have different addresses
until the linker happens.
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2025-04-27 08:50:55