获得海湾合作委员会"no matching function"

Got "no matching function" from GCC

本文关键字:no matching function 委员会 得海      更新时间:2023-10-16

为什么这个简单的代码不起作用?

template<class U>
class retype 
{ 
    typedef U type; 
};
class object
{
public:
    template<class U>
    int create(typename retype<U>::type p)
    {
        return 4;
    }
};
int main()
{
    int n = object().create(5);
    return 0;
}

使用GCC编译时出现这个错误:

test.cpp: In function ‘int main()’:
test.cpp:20: error: no matching function for call to ‘object::create(int)’

问题在哪里?

您依赖于从函数参数中推导模板参数。但是函数模板实参不能推导,因为它是一个不可推导的上下文。

更具体地说,即使retype<U>::typeint,模板参数U也不能推导出来。因为retype的专门化可能定义为:

template<>
struct retype<X>
{
      typedef int type;
};

所以你看,给定retype<U>::typeint,模板参数U也可以是X

实际上,可能有不止一个这样的专门化,它们都可以将type定义为int。所以没有一对一的关系。编译器不能唯一地推导出U

相关文章: