dlang最新dmd2103版本如下代码:import stdstdio;void printInfoTint numref T name writelnnamenum;void main printInfo!intstring2two;编译出错:Error template instance printInfo!int string does not match template d
根据错误提示,可以看出模板实例 printInfo!(int,string) 和模板声明 printInfo(T)(int num,ref T name) 不匹配。这是因为在模板实例化时,类型 int 和类型 string 不满足模板声明中的 T 类型参数的要求。
解决方法是将类型参数 T 的要求放宽,即将模板声明中的 ref T 改为 T,代码如下:
import std.stdio;
void printInfo(T)(int num, T name)
{
writeln(name, ":", num);
}
void main()
{
printInfo!(int, string)(2, "two");
}
这样就可以正确编译和运行了。注意在调用模板函数时,需要使用感叹号 ! 指定模板参数类型。
原文地址: https://www.cveoy.top/t/topic/fhUa 著作权归作者所有。请勿转载和采集!