- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下代码在 Visual C++ 和 gcc 中编译,但在 Code Warrior 中失败
提示是对模板的调用不明确——无法在 doIt( M* ) 和 doIt( M const* ) 之间做出决定,即使在每种情况下,参数明确地是成本或非常量。令人恼火的是,如果我提供第二个模板参数,它会认为它不再有歧义。
template< typename T1, typename T2 >
T1 const* doIt( T2 const* );
template< typename T1, typename T2 >
T1* doIt( T2* );
class M {};
class N : public M {};
void f()
{
M* m1 = NULL;
M const* m2 = NULL;
doIt<N>( m1 ); // Fail
doIt<N>( m2 ); // Fail
doIt<N,M>( m1 ); // OK
doIt<N,M>( m2 ); // OK
}
这只是 Code Warrior 编译器的错误吗? (或者 gcc/Visual C++ 出错)。
最佳答案
这是 codewarrior 编译器的错误。
这是应该发生的事情:
template< typename T1, typename T2 >
T1 const* doIt( T2 const* ); // 1
template< typename T1, typename T2 >
T1* doIt( T2* ); // 2
class M {};
class N : public M {};
void f()
{
M* m1 = 0;
M const* m2 = 0;
doIt<N>( m1 );
// In the above call - the compiler does the following (post argument deduction)
// 1) create a viable set of functions { N* doIt1<N,M>(const M*) , N* doIt2<N, M>(M*) }
// 2) check the conversion sequences - M* -> M* is better than M* -> const M*
// Since doIt2 has a "better" conversion sequence (hard to beat identity) it wins - no ambiguity
doIt<N>( m2 );
// 1) Viable functions: { doIt1<N,M>(const M*), doIt2<N,const M>(const M*) }
// 2) Conversion Sequence Ranking: both do identity - so both are good
// 3) Check to see if the "mother" template of either candidate is more specialized
// - Since doIt1 theoretically matches fewer types than doIt2, it is unambiguously more specialized (the standard specifies an algorithm to check this)
// - so doIt1 wins
}
希望对您有所帮助。
关于c++ - 模棱两可的模版,代码战士,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1413588/
我从一所大学获得了一些示例代码,导入了项目并尝试运行测试:方法 assertThat(Integer, Matcher) 对于 MyClass 类型是不明确的 每个 assertThat 都被标记为红
关于将 iOS 应用程序迁移到 Swift 3.0 的过程。这是我面临的一个问题。 先上相关代码: let calendar = NSCalendar.current, calendCompo = c
我刚开始研究 Java 8 Lambda 功能。我在 Java 7 中编写了这段代码,并尝试在 lamdas 中执行它。请注意,最后一行会产生编译错误,因为重载的函数不明确。我明白原因。如何使用 la
如何优先(告诉编译器)使用“函数接收引用”(#B)而不是“函数接收值”(#A)? #include using namespace std; class Heavy{/* ...... */}; /
我正在使用 Google Map API V3 显示车辆行驶路径及其路线方向。但是通过谷歌方向图标,很难找到方向。下图解释更多 我看到了每个图标,它是 source 我找到了图片路径,是 http:/
我是一名优秀的程序员,十分优秀!