- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
编译时
void ambig( signed long) { }
void ambig(unsigned long) { }
int main(void) { ambig(-1); return 0; }
我明白了
error C2668: 'ambig' : ambiguous call to overloaded function
could be 'void ambig(unsigned long)'
or 'void ambig(long)'
while trying to match the argument list '(int)'
我知道我可以通过说 -1L
而不是 -1
来“修复”它,但是为什么/为什么这首先被认为是模棱两可的?
最佳答案
您正在将 int
传递给这个重载函数。
虽然人类的直觉认为 ambig(signed long)
应该是首选,因为您的输入是一个负整数(不能用 unsigned long
表示) ,这两个转换在 C++ 中的“优先级”实际上是等价的。
也就是说,转换 int
→ unsigned long
被认为与 int
→ signed long
一样有效code>,两者都不如对方好。
另一方面,如果您的参数已经是 long
而不是 int
,则 完全匹配 signed long
,无需转换。 This avoids the ambiguity .
void ambig( signed long) { }
void ambig(unsigned long) { }
int main(void) { ambig(static_cast<long>(-1)); return 0; }
“只是其中之一”。
[C++11: 4.13/1]:
("Integer conversion rank")Every integer type has an integer conversion rank defined as follows:
- [..]
- The rank of a signed integer type shall be greater than the rank of any signed integer type with a smaller size.
- The rank of
long long int
shall be greater than the rank oflong int
, which shall be greater than the rank ofint
, which shall be greater than the rank ofshort int
, which shall be greater than the rank of signed char.- The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.
- [..]
[ Note: The integer conversion rank is used in the definition of the integral promotions (4.5) and the usual arithmetic conversions (Clause 5). —end note ]
重载决议很复杂,定义在[C++11: 13.3]
;我不会在这里引用其中的大部分内容来让您感到厌烦。
不过这里有一个亮点:
[C++11: 13.3.3.1/8]:
If no conversions are required to match an argument to a parameter type, the implicit conversion sequence is the standard conversion sequence consisting of the identity conversion (13.3.3.1.1).
[C++11: 13.3.3.1/9]:
If no sequence of conversions can be found to convert an argument to a parameter type or the conversion is otherwise ill-formed, an implicit conversion sequence cannot be formed.
[C++11: 13.3.3.1/10]:
If several different sequences of conversions exist that each convert the argument to the parameter type, the implicit conversion sequence associated with the parameter is defined to be the unique conversion sequence designated the ambiguous conversion sequence. For the purpose of ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous conversion sequence is treated as a user-defined sequence that is indistinguishable from any other user-defined conversion sequence134. If a function that uses the ambiguous conversion sequence is selected as the best viable function, the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous.
/10
是您遇到的情况; /8
是您使用 long
参数的情况。关于c++ - 为什么用整数文字调用重载的 ambig(long) 和 ambig(unsigned long) 会产生歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935189/
编译时 void ambig( signed long) { } void ambig(unsigned long) { } int main(void) { ambig(-1); return 0
编译时 void ambig( signed long) { } void ambig(unsigned long) { } int main(void) { ambig(-1); return 0
我用了this brilliant solution将 linq 查询转换为数据表。但是我在运行它时遇到了一个奇怪的错误。构建成功。 Compiler Error Message: CS0121: T
#include "stdafx.h" #include using namespace std; void func(void); static int count = 10; /* Global
我有一个模拟指针行为的类的模板,在转换为任何指针类型时返回指向给定存储内存位置的指针。 它具有转换为多种类型的运算符(intptr_t、它正在模拟的 native 指针等),对于指针来说是正常的,并且
我正在使用spark数据框,读取JSON数据,然后将其保存到orc。代码很简单: DataFrame json = sqlContext.read().json(input); json.write(
当我导入alamofire源时,一开始一切正常,但是当我添加创建桥接头文件到项目后,发生错误: see error picturs please我尝试删除桥接文件并重置build设置->objecti
此代码在 VS2010 中编译,我相信它适用于任何编译器。 #include using namespace std; class ostream; int main() {} 这段代码也是如此 #
在 §8.2[dcl.ambig.res]/2 中,我们有以下注释(重点是我的): [ Note: A declaration can be explicitly disambiguated by a
正如标题所说,我在用 C++ 调用我的方法时遇到了问题。我试图自己解决它我只是卡住了所以我决定在这里发布问题。 那么有人可以告诉我我做错了什么吗?提前致谢! 我的代码: #include "stdaf
这个初始化给了我一个错误: Ambiguous use of 'init(URL:options:)' let sceneSource = SCNSceneSource(URL: url, optio
我是一名优秀的程序员,十分优秀!