Re: Address identity of functions
Am 08.10.2011 01:55, schrieb Ruslan Abdikeev:
On Oct 6, 11:49 am, Daniel Kr?gler<daniel.krueg...@googlemail.com>
wrote:
static_assert(
&function_to_object<f>::data !=&function_to_object<g>::data,
"Function identity violation");
This assertion would no longer be guaranteed to hold.
The expression in static_assert is not a compile-time expression, so
this scenario wouldn't affect any well-formed programs anyway.
According to the C++11 standard this is definitively a constant expression:
function_to_object<f> and function_to_object<g> are just specific types, and all (non-polymorphic) types are known at compile-time. Replace them by typedefs A and B for this discussion, we have the effective expression
&A::data != &B::data
to consider.
So, what we are doing here within the static_assert declaration is to evaluate the address of two objects of static storage duration, which are instantiated in the same translation unit.
This is more or less equivalent to (in namespace scope):
int a;
int b;
static_assert(&a != &b, "Object identity violation");
Looking at 5.19 p2 there is no exclusion case matching here (&a and &b are both address-constant expressions). In particular
"a relational (5.9) or equality (5.10) operator where the result is unspecified;"
does not hold, because the result of the unequal comparison *is* specified, see 5.10 p1
"Pointers of the same type [..] can be compared for equality. Two pointers of the same type compare equal if and only if they [..] both represent the same address (3.9.2)."
following 3.9.2 p3 and 1.7 which clarifies that a and b have different memory locations and thus different addresses.
Thus, with the return type of the built-in != being of bool type, the expression
&a != &b
or
&function_to_object<f>::data != &function_to_object<g>::data
are integral constant expressions, respectively.
HTH & 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! ]