Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an implementation allowed to site two identical function definitions at the same address, or not? [duplicate]

Tags:

The C++ standard says the following about the equality operator ==:

[C++11: 5.10/1]: [..] Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address.

My initial interpretation was that functions don't semantically have "addresses" per se at this level, and that therefore the "or both represent the same address" could only be intended to refer to objects, and not functions. Otherwise why bother with the "point to the same function" clause?

That is, two function pointers of the same type compare equal if and only if both point to the same function, period.

The consequence of this would be that the behaviour witnessed in this question (pointers to two distinct but identical functions have identical values) would be an implementation bug, since pointers to distinct functions would be required to be unique.

I feel that this is the intent of the clause, but I can't find a way to objectively defend the viewpoint that this is how the passage's meaning should actually be inferred, or that it really was the intent of the committee, and now my interpretation has come into question:

[D]iscuss with me how "[...] or both represent the same address." is not being satisfied by Visual C++'s behavior. (@jstine)

So my question is about the intent of this standard passage.

Either:

  • I am on the right track: function pointers must compare equal iff they both point to the same function ("addresses" be damned), or

  • There is a redundancy in the passage: function pointers must compare equal iff they both point to the same function or both represent the same address; and, by extension, an implementation is allowed to make two functions exist at the same address.

Which is it?

like image 701
Lightness Races in Orbit Avatar asked Jan 07 '13 01:01

Lightness Races in Orbit


1 Answers

Well, look at the statement logically. You have three clauses:

  1. Both are null.

  2. Both point to the same function.

  3. Both have the same address.

These clauses are joined by a logical "or". Therefore, if any one of these is true, then the two pointers are allowed to compare equal. If a compiler so decides, it is possible to fail #3 yet still pass #2. Logical "or" means that such pointers would compare equal.

Also, it should be noted that member pointers do not have an "address" in the traditional sense. They do have a value, but it's not a memory address. That's why you're not allowed to cast them to void* and so forth.

The passage guarantees, given function pointers t and u, if t == u, that t(...); shall cause the same behavior as u(...);. That behavior will either be referencing NULL, calling the same function, or executing the code at the same address. Thus, the same behavior is had.

Technically, Mehrdad's problem is that he's getting the same value from two different member function names. So #3 applies. I don't see anything in the standard that requires that different member function names return distinct values when getting functions for them.

like image 125
Nicol Bolas Avatar answered Oct 04 '22 16:10

Nicol Bolas