C++ 编译器对模板的实例化无效,出现错误
它为什么这样做?当有两个必需参数时,编译器不应将C++ 编译器对模板的实例化无效,出现错误,c++,templates,recursion,C++,Templates,Recursion,它为什么这样做?当有两个必需参数时,编译器不应将100用于可变参数 它还会尝试使用Rest={const char*,int,const char*,int,float}创建模板函数吗 代码是否有错误,或者变量参数不是这样使用的 编译器g++11.1.0(也给出了与clang相同的错误)问题是,在最后的递归调用中,rest对于printf\u vt(s,rest…)变为空;而且没有要调用的候选者printf\u vt 您可以添加一个重载的printf_vt只取const char*来匹配上面的场
100
用于可变参数
它还会尝试使用Rest={const char*,int,const char*,int,float}
创建模板函数吗
代码是否有错误,或者变量参数不是这样使用的
编译器g++11.1.0(也给出了与clang相同的错误)问题是,在最后的递归调用中,
rest
对于printf\u vt(s,rest…)变为空代码>;而且没有要调用的候选者printf\u vt
您可以添加一个重载的printf_vt
只取const char*
来匹配上面的场景,它还应该终止递归调用。例如
Rest = {int , const char*, int , float} //{100 , "more" , x , y}
void printf_vt(常量字符*s)
{
std::cout模板中缺少递归的基本情况。编译器告诉您它无法用Rest={}实例化模板
提供基本案例的一种方法是也使用no-Rest
模板。Add此模板:
void printf_vt(const char *s)
{
std::cout << s;
}
模板
void printf_vt(常量字符*s,T值)
{
而(*s){
如果(*s=='%'&&*(++s)!='%'){
std::cout@songyuanyao的答案更短、更切题,只有一个重载。基本函数中的std::cout
似乎没有用处。s=\0@nhatnq不,它是\n
,我想应该打印出来。@nhatnq它将在最后一个字符串之后打印字符串的其余部分argument@songyuanyao谢谢你的回答。你知道mea吗这是什么?类myexception:public exception{virtual const char*what()const throw(){返回“我的异常发生”}
@rog这是一个类定义。你不知道哪一部分?
Rest = {int , const char*, int , float} //{100 , "more" , x , y}
void printf_vt(const char *s)
{
std::cout << s;
}
template<typename T>
void printf_vt( const char *s, T value)
{
while (*s) {
if (*s == '%' && *(++s) != '%') {
std::cout << value << s;
return;
}
std::cout << *s++;
}
}