忽略候选模板,因为无法推断模板参数 [英] Candidate template ignored because template argument could not be inferred
问题描述
下面的代码有什么问题?
#include< iostream>
template< typename K>
struct A {
struct X {K p; };
struct Y {K q; };
};
template< typename K>
void foo(const typename A< K> :: X& x,const typename A< K> :: Y& y){
std :: cout< A<< std :: endl;
}
int main(){
A< float> :: X x;
A< float> :: Y y;
foo(x,y);
}
clang提供以下错误消息:
17:2:错误:没有匹配的函数调用'foo'
foo(x,y);
^ ~~
10:6:注意:候选模板被忽略:无法推断模板参数'K'
void foo(const typename A< K> :: X& x,const typename A< K> :: Y& y){
^
产生1个错误。
参数 通过运行这个思想实验来请求扣除是没有意义的。 没有从类型到嵌套类型的一对一映射:给定任何类型code> int code>在
),可能有许多环境类型,它是一个嵌套类型,或不需要任何。 const类型名A< K> :: X
是不可推断的 。基本上, ::
剩下的一切都不可推导(如果 ::
分隔一个嵌套名称)。
struct A {typedef int type; }
struct B {typedef int type; }
template< typename T> void foo(typename T :: type);
foo(5); // is T == A or T == B ??
What is wrong with the following piece of code?
#include <iostream>
template<typename K>
struct A {
struct X { K p; };
struct Y { K q; };
};
template<typename K>
void foo(const typename A<K>::X& x, const typename A<K>::Y& y) {
std::cout << "A" << std::endl;
}
int main() {
A<float>::X x;
A<float>::Y y;
foo(x, y);
}
clang gives the following error message:
17:2: error: no matching function for call to 'foo'
foo(x, y);
^~~
10:6: note: candidate template ignored: couldn't infer template argument 'K'
void foo(const typename A<K>::X& x, const typename A<K>::Y& y) {
^
1 error generated.
The argument K
in const typename A<K>::X
is not deducible. Basically, everything left of a ::
is not deducible (if ::
separates a nested-name).
It's trivial to see why it makes no sense to ask for deduction by running through this thought experiment:
struct A { typedef int type; }
struct B { typedef int type; }
template <typename T> void foo(typename T::type);
foo(5); // is T == A or T == B ??
There's no one-to-one mapping from types to nested types: Given any type (such as int
), there could be many ambient types of which it is a nested type, or there needn't be any.
这篇关于忽略候选模板,因为无法推断模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!