本文介绍了是否允许实现在同一地址的两个相同的函数定义,或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

C ++标准说明如下关于等于运算符 ==

我的初步解释是函数不语义在该级别具有地址本身,因此或两者表示相同地址仅能够用于指代对象,而不是功能。否则,为什么要用指向同一个函数子句呢?



也就是说,两个相同类型的函数指针当且仅当两个指向同一个功能,期间。



这样做的后果是,在(指向两个不同但相同的函数的指针具有相同的值)将是一个实现错误,因为指向不同函数的指针将需要是唯一的。



感觉这是子句的意图,但我找不到一种方式客观地保护观点,这是如何的通道的意思应该实际推断,或者它真的是委员会的意向,现在我的解释:

所以我的问题是关于这个标准段落的意图。






  • 我在正确的轨道上:函数指针必须比较 iff 他们都指向同一个函数(地址被诅咒),或


  • 函数指针必须比较等于 iff ,它们都指向同一个函数,或者两者都表示相同的地址;




其中是吗?

解决方案

您有三个子句:


  1. 两者皆为空。



$

b
$ b

这些子句通过逻辑或连接。因此,如果这些中的任何一个为真,则两个指针被允许比较相等。如果编译器这样决定,可能失败#3但仍然通过#2。逻辑或意味着这样的指针将比较相等。



此外,应注意成员指针在传统意义上没有地址。他们有一个值,但它不是一个内存地址。这就是为什么你不允许把它们转换为 void * 等等。



函数指针 t u ,如果 t == u t(...); 将导致与 u(...); 相同的行为。该行为将引用NULL,调用相同的函数,或在相同的地址执行代码。因此,也有同样的行为。



技术上来说,是他从两个不同的成员函数名获得相同的值。所以#3适用。我在标准中没有看到任何要求不同的成员函数名称在获取函数时返回不同的值。


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

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:

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?

解决方案

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.

这篇关于是否允许实现在同一地址的两个相同的函数定义,或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:04