gpt4 book ai didi

vb.net - Visual Studio 2010 与 Visual Studio 2013 - 不同的编译器错误 - 为什么?

转载 作者:行者123 更新时间:2023-12-02 09:48:32 25 4
gpt4 key购买 nike

我们注意到一些我们无法解释的奇怪现象。在一个类中,我们有这两个函数:

   Public Overloads Shared Function ToList(ByVal input As Object, _
Optional ByVal StringSeparator As String = ";", _
Optional ByVal CharacterCasing As String = "", _
Optional ByVal StartRow As Integer = 0, _
Optional ByVal EndRow As Integer = -1) As String
...
End Sub

Public Overloads Shared Function ToList(ByVal Input As Object, _
Optional ByVal SplitStringSeparator As String = ",", _
Optional ByVal JoinStringSeparator As String = ",", _
Optional ByVal PreFixStr As String = "", _
Optional ByVal PostFixStr As String = "") As String
...
End Sub

如果我们尝试在 VS2013 中编译此项目,我们不会收到任何错误。如果我们尝试在 VS 2010 中编译这个完全相同的项目,则会出现以下错误(这是有道理的):

Error 1 'Public Shared Overloads Function ToList(input As Object, [StringSeparator As String = ";"], [CharacterCasing As String = ""], [StartRow As Integer = 0], [EndRow As Integer = -1]) As String' and 'Public Shared Overloads Function ToList(Input As Object, [SplitStringSeparator As String = ","], [JoinStringSeparator As String = ","], [PreFixStr As String = ""], [PostFixStr As String = ""]) As String' cannot overload each other because they differ only by the default values of optional parameters

Error 2 'Public Shared Overloads Function ToList(input As Object, [StringSeparator As String = ";"], [CharacterCasing As String = ""], [StartRow As Integer = 0], [EndRow As Integer = -1]) As String' and 'Public Shared Overloads Function ToList(Input As Object, [SplitStringSeparator As String = ","], [JoinStringSeparator As String = ","], [PreFixStr As String = ""], [PostFixStr As String = ""]) As String' cannot overload each other because they differ only by the types of optional parameters.

任何人都可以很好地解释为什么在不同版本的 VS 中打开包含 SAME 项目的 SAME 解决方案时会出现差异,有时会出现错误,有时不会?

最佳答案

查看通过引用源提供的编译器源代码,我认为它接受第二个函数作为有效重载,因为默认值具有不同的类型。你可以自己看一下,vb\language\compiler\symbols\symboltable.cpp,BCSYM::CompareParams()函数。

    // make sure the types being used for the default are the same
if ( ParmTypeCompareFlags & ( EQ_Shape | EQ_GenericTypeParams ))
{
ComparisonFlags |= EQ_OptionalTypes;

// don't percolate the difference in the type shape on in this case - the difference we care about it is EQ_OptionalTypes
ParmTypeCompareFlags &= ~(EQ_Shape | EQ_GenericTypeParams); // <== Here
}

关闭EQ_Shape是一个bug,表明参数不同。我无法将其与 VS2010 中的完成方式进行比较,该版本不可用。

这当然没有道理。它违反了语言规范第 4.1.1 章的规定:

A method with optional parameters is considered to have multiple signatures, one for each set of parameters that can be passed in by the caller. For example, the following method has three corresponding signatures:

Sub F(x As Short, _
Optional y As Integer = 10, _
Optional z As Long = 20)

These are the method's signatures:
• F(Short)
• F(Short, Integer)
• F(Short, Integer, Long)

所以我认为您发现了一个错误。这是一个温和的问题,无论如何它都会在调用站点得到解决,您会收到错误 BC30521,“过载解析失败”。您可以在 connect.microsoft.com 上报告该问题

关于vb.net - Visual Studio 2010 与 Visual Studio 2013 - 不同的编译器错误 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23254882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com