忽略候选模板,因为无法推断模板参数 [英] Candidate template ignored because template argument could not be inferred

查看:587
本文介绍了忽略候选模板,因为无法推断模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码有什么问题?

  #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>在 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 ??

没有从类型到嵌套类型的一对一映射:给定任何类型code> int ),可能有许多环境类型,它是一个嵌套类型,或不需要任何。


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屋!

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