我有一个像这样的课程
template <class T>
struct A{
template <class U>
A(U u);
};
我想为这样的声明编写一个明确的专业化
A<int>::A(float);
在下面的测试代码中,如果我注释掉专业化,它将使用 g++ 进行编译。否则,它会说我的模板参数数量错误:
#include <iostream>
template <class T>
struct A{
template <class U>
A(T t, U *u){
*u += U(t);
}
};
template <>
template <>
A<int>::A<int,float>(int t, float *u){
*u += float(2*t);
}
int main(){
float f = 0;
int i = 1;
A<int>(i, &f);
std::cout << f << std::endl;
return 0;
}
Try
template <>
template <>
A<int>::A(int t, float *u){
*u += float(2*t);
}
这似乎对我有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)