编译器模糊错误不一致 [英] Inconsistencies in compiler ambiguity errors

查看:301
本文介绍了编译器模糊错误不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题中,定义了以下类:

  public class Program 
{
int n = 0;

public void Print()
{
Console.WriteLine(n);
}

public Program()
{
}

public Program(int num = 10)
{
n = num;
}
}



现在,显然一个调用构造函数使用 new Program()是不明确的,因为第二个构造函数的可选参数。当读取它不清楚是否 n 应使用 0 10



但是:



问题的实际报告似乎不一致。设置这个类并使用它我能够在C#或VB.Net中使用 new Program(),在.Net 4或.Net 4.5中。所有测试都在VS2013中完成。



在链接问题中,当使用VB.Net IN .NET 4和VS2010时,OP实际上得到错误消息:



>

那么为什么有时会出现错误,而在其他时候程序可以成功执行?



当执行成功时 n 的值为0.

解决方案

因为VB语言在版本10和 11 之间更改。在第11版语言规范中,第11.8.1节。添加了拆分规则7.9:


7.9。如果M没有使用任何可选的参数默认值来代替显式参数,但N没有使用,那么从集合中删除N.


正如规则7.8和7.10,但在这里不相关)


In this question, the following class is defined:

public class Program
{
    int n = 0;

    public void Print()
    {
        Console.WriteLine(n);
    }

    public Program()
    {
    }

    public Program(int num = 10)
    {
        n = num;
    }
}

Now, obviously a call to the constructor here using new Program() is ambiguous due to the optional parameter on the second constructor. When reading this its not clear whether n should be initialized with 0 or 10.

However:

Actual reporting of the problem seems inconsistent. Setting up this class and using it I was able to use new Program() in either C#, or VB.Net, and in .Net 4 or .Net 4.5. All tests were done in VS2013.

In the linked question though, the OP actually got the error message when using VB.Net IN .NET 4 and VS2010:

'.ctor' is ambiguous because multiple kinds of members with this name exist in class 'ConsoleApplication2.Program'.

So why is the error there sometimes while at other times the program can be executed successfully?

For reference the value of n when the execution is successful is 0.

解决方案

Because the VB language changed between versions 10 and 11. In the Version 11 Language Specification, Section 11.8.1. Tie breaking rule 7.9 was added:

7.9. If M did not use any optional parameter defaults in place of explicit arguments, but N did, then eliminate N from the set.

(As were rules 7.8 and 7.10, but irrelevant here)

这篇关于编译器模糊错误不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆