Re: Address identity of functions
Am 05.10.2011 23:38, schrieb A. McKenney:
On Oct 4, 2:23 pm, Daniel Kr?gler<daniel.krueg...@googlemail.com>
wrote:
Am 03.10.2011 20:57, schrieb Zeljko Vrba:
On 2011-10-02, Daniel Kr?gler<daniel.krueg...@googlemail.com> wrote:
There are some compilers that deduce from this, that the following
program is allowed to output '1' instead of '0':
[code which compares pointers to two empty functions with the same
signature.]
Did you check whether this is the case when both functions are
non-empty, but
have identical code?
I haven't but I have been told that this happens even with non-empty
functions
(This makes this actually of interest, because you can effectively reduce
the
code bloat, especially from template instantiations).
What about:
#include<iostream>
int f(){ static int i = 0; return ++i; }
int g(){ static int i = 0; return ++i; }
int main()
{
std::cout<< (&f ==&g)<< std::endl;
std::cout<< f()<< std::endl;
std::cout<< g()<< std::endl;
}
f() and g() have identical code, but you would
certainly be able to tell if it was treating f() and
g() as the same function.
That is a good example and it is interesting to note that VS2010 does
output
0
1
1
so local state seems to be honored for this compiler.
Nonetheless, if address identity would be given up, there would be some
normative statements required that ensure that this is guaranteed for
such examples.
But as you mention observability: It would be definitively possible to
construct more complex examples that won't no longer work, e.g. if
someone used function address values as keys of some mapping as in the
following example:
template<void(*)()>
struct function_to_object {
static int data;
};
template<void(*P)()>
int function_to_object<P>::data;
void f() {}
void g() {}
static_assert(
&function_to_object<f>::data != &function_to_object<g>::data,
"Function identity violation");
This assertion would no longer be guaranteed to hold. Does anyone take
advantage of such form of function mapping?
Thanks & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]