且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在方法覆盖更改params修饰符

更新时间:2023-12-04 20:44:40

编译器的行为是正确的,但是这是一个有点乱。我宁愿这至少是一个警告。

The compiler's behaviour is correct, but this is a bit of a mess. I would have preferred this to be at least a warning.

这是不足为奇的,你找不到的地方在规范它说,这是正确的。相关位是:

It is unsurprising that you can't find where in the spec it says this is correct. The relevant bits are:

形式M(A)中,其中M是一个方法组的一个方法调用的结合时处理,A是一个可选的参数列表,包括以下步骤:该套候选方法方法调用构造。对于使用方法M组相关联的每个方法F.如果F是不通用的,F是候选当M没有类型参数列表,和F是适用的为A

The binding-time processing of a method invocation of the form M(A), where M is a method group, and A is an optional argument-list, consists of the following steps: The set of candidate methods for the method invocation is constructed. For each method F associated with the method group M, if F is non-generic, F is a candidate when M has no type argument list, and F is applicable with respect to A.

什么是与方法M组相关的方法?嗯,首先,什么是方法组?

What are the "methods associated with the method group M"? Well, first off, what is a method group?

方法组,这是一组由成员查找产生的重载方法。 ..

A method group, which is a set of overloaded methods resulting from a member lookup...

确定,因此哪些成员查找规则?

OK, so what are the member lookup rules?

否则,该集由T中名为N的所有可访问的成员,包括继承的成员并命名为N的对象可访问成员。

Otherwise, the set consists of all accessible members named N in T, including inherited members and the accessible members named N in object. Members that include an override modifier are excluded from the set.

着重强调的成员,其中包括一个override修饰符被排除在集。

Emphasis added.

下面的实际结果是,作为重载决议的目的,一个重写的方法被认为是是的最初宣布方法>,而不是方法是覆盖这条规则是,不幸的是,违反了在这种情况下:

The practical upshot here is that for the purpose of overload resolution, an overridden method is considered to be the method that was originally declared, not the method that was overriding. This rule is, unfortunately, violated in this case:

virtual void M(int x, int y) { }
...
override void M(int y, int x) { } 
...
M(x = 1, y = 2);



重载使用的名称的距离的更多衍生版本。这是一个事实,即命名参数添加在游戏很晚了不幸的后果。

Overload resolution uses the names from the more derived version. This is an unfortunate consequence of the fact that named arguments were added very late in the game.

在短:用于确定一个方法是否是PARAMS与否的目的,分析是在原始方法的,不这样做上的重写方法的。

In short: for the purposes of determining whether a method is "params" or not, the analysis is done on the original method, not on the overriding method.

这本来是很好过的编译器在这里给你一个警告。

It would have been nice had the compiler given you a warning here.

可以说,至少在Visual Studio开发环境是混淆了这个

can say that at least the Visual Studio development environment is confused about this

正确的。智能感知层始终显示压倒一切的方法,而不是覆盖方法方法信息。研究表明,用户发现它令人困惑的时候作了方法看起来好像他们是最初宣布的方法,而不是压倒一切的方法。当然,正如我前面提到的,那些是你要使用的命名参数的参数名。

Correct. The IntelliSense layer always displays the method information for the overriding method, not the overridden method. Research showed that users found it confusing when methods were made to look as though they were the originally declaring method, not the overriding method. And of course, as I mentioned before, those are the parameter names you're going to use for named arguments.