Is the implementation allowed to host two identical function definitions at the same address or not?

The C ++ standard talks about the equality operator == :

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

My initial interpretation was that functions do not have semantically “addresses” as such at this level, and therefore “or both represent the same address” can only be used to refer to objects, not functions. Otherwise, why bother with the article "point to the same function"?

That is, two function pointers of the same type are compared in the same way, if and only if both indicate the same function, period.

The consequence of this would be that the behavior described in this question (pointers to two different but identical functions have the same meaning) would be an implementation error, since pointers to individual functions would need to be unique.

I feel that this is the goal of the proposal, but I can’t find a way to objectively defend the point of view that this is the meaning of the passage, or that this was really the goal of the committee, and now my interpretation has called into question :

[D] refers to me as "[...] or both represent the same address." not satisfying Visual C ++ behavior. (@jstine)

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

Or:

  • I am on the right track: function pointers should be compared if they both point to the same function (the “addresses” will be damned) or

  • There is redundancy in the pass: function pointers must compare the same if both of them point to the same function or both represent the same address; and, in turn, the implementation is allowed to create two functions at the same address.

What is it?

+24
c ++ language-lawyer
Jan 07 '13 at 1:14
source share
1 answer

Ok, look at the expression logically. You have three conditions:

  • Both values ​​are zero.

  • Both indicate the same function.

  • Both have the same address.

A logical "or" is attached to these sentences. Therefore, if any of them is true, then two pointers are allowed to compare the same. If the compiler so decides, you can skip # 3, but still go through # 2. The logical “or” means that such pointers will compare equal.

In addition, it should be noted that element pointers do not have an “address” in the traditional sense. They matter, but it is not a memory address. This is why you are not allowed to send them to void* , etc.

Passing guarantees if pointers to the functions t and u , if t == u , then t(...); will lead to the same behavior as u(...); . This behavior will either refer to NULL, call the same function, or execute code at the same address. So the same behavior was.

Technically, Mehrdad 's problem is that it gets 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 different values ​​when getting functions for them.

+5
Jan 07 '13 at 2:29
source share



All Articles