重载运算符“ =”匿名访问类型? [英] Overloading operator "=" for anonymous access types?
问题描述
我正在研究巴恩斯的出色Ada书。这是一个用于对第11.7节中的链表进行深度比较的代码示例:
I am working my way through Barnes' excellent Ada book. This is a code sample for deep comparison of linked lists from section 11.7:
type Cell is
record
Next: access Cell;
Value: Integer;
end record;
function "=" (L, R: access Cell) return Boolean is
begin
if L = null or R = null then -- universal =
return L = R; -- universal = (Line A)
elsif L.Value = R.Value then
return L.Next = R.Next; -- recurses OK (Line B)
else
return False;
end if;
end "=";
我似乎无法绕开A行运算符 =的原因。 (由于使用了优先规则),被称为Universal_access类型的 ,但是,调用了用户定义的运算符 = (首先使得递归成为可能) ),这一次不希望使用Universal_access的运算符 =。
I can't seem to wrap my head around why in Line A operator "=" of the universal_access type is called (because of the preference rule), on Line B, however, the user-defined operator "=" is called (which makes recursion possible in the first place), this time with no preference for operator "=" of universal_access.
L和R以及L.Next和R.Next都具有相同的匿名性输入访问单元。为什么派遣有所不同?它与L和R是访问参数有关吗?如果是这样,那里的规则是什么?
Both L and R, as well as L.Next and R.Next are of the same anonymous type "access Cell". Why the difference in "dispatching"? Does it have to do with L and R being access parameters? If so, what is the rule there?
我竭尽全力在AARM中找到任何内容,尤其是第4.5.2节,但没有任何意义。
I did my best to find anything in the AARM, especially section 4.5.2, but could not make any sense of it.
干杯。
推荐答案
我将总结我的发现(与到目前为止,还是Simon Wright和G_Zeus的帮助)。如果我错了,请纠正我:
I will summarize my findings (with the help of Simon Wright and G_Zeus) as of now. Please correct me if I'm wrong:
根据标准, L = null
, R = null
, L = R
以及 L.Next = R.Next
每个都应明确调用用户定义的运算符=。 universal_access
运算符=绝对不能插入。
According to the standard, L = null
, R = null
, L = R
as well as L.Next = R.Next
should each unambiguously call the user-defined operator =. universal_access
operator = must not kick in at all here.
原因:
操作数 L
, R
, L.Next
和 R.Next
违反了 ARM 4.5.2(9.1-9.4)用于解释其中的 =
表示 universal_access
类型的运算符=的表达式:
The operands L
, R
, L.Next
and R.Next
violate the precondition in ARM 4.5.2(9.1-9.4) for interpreting =
in these expressions as to mean operator = of the universal_access
type:
前提条件是两个操作数都不是对象类型( access Cell
)的访问类型,其指定类型为 Cell
( check ), Cell
具有用户定义的原始等式运算符( check ),使得
The precondition says that neither of the operands are of an access-to-object type (access Cell
) whose designated type is Cell
(check), Cell
has a user-defined primitive equality operator (check) such that
- 其结果类型为
Boolean
( check ); - 立即在与
Cell
( check )相同的声明列表中声明;并且 - 至少一个操作数是具有指定类型
Cell
的访问参数(两个操作数均为 check )。
- its result type is
Boolean
(check); - it is declared immediately within the same declaration list as
Cell
(check); and - at least one of its operands is an access parameter with designated type
Cell
(both operands are, check).
operator =的 universal_access
类型的优先规则在 ARM 8.6(29.1)不适用于此处,因为它需要两个可接受的解释。但是由于4.5.2, universal_access
类型的operator =不能被接受。
The preference rule for operator = of the universal_access
type in ARM 8.6(29.1) does not apply here, since it requires "two acceptable interpretations". But because of 4.5.2, operator = of the universal_access
type is not an acceptable interpretation.
别无选择:在所有情况下(甚至 L = null
),它都必须是用户定义的运算符=。
So there is no choice: in all cases (even L = null
) it has to be the user-defined operator =.
@Simon Wright:所以无界递归实际上是正确的编译器行为。
@Simon Wright: So "unbounded recursion" is actually the correct compiler behavior.
@G_Zeus:为 l = r <发出歧义错误/ code>是错误的编译器行为,编译器应选择
Access_Equal。 =
。
@G_Zeus: Issuing an ambiguity error for l = r
is incorrect compiler behavior, the compiler should have picked Access_Equal."="
.
该示例应正确读取:
...
if Standard."="(L, null) or Standard."="(R, null) then -- universal =
return Standard."="(L, R); -- universal =
elsif L.Value = R.Value then
return L.Next = R.Next; -- recurses OK
...
干杯。
这篇关于重载运算符“ =”匿名访问类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!