【发布时间】:2011-11-28 21:15:27
【问题描述】:
我正在尝试在 T 类型的模板类中显式实例化 U 类型的模板函数。下面的代码会生成警告,并且链接器没有找到 ReinterpretAs() 的显式实例化。任何人都可以发现错误或建议如何执行此操作吗?我正在使用 VC++ 2010。
template<typename T>
class Matrix
{
public:
template<typename U> Matrix<U> ReinterpretAs() const;
};
template<typename T>
template<typename U>
Matrix<U> Matrix<T>::ReinterpretAs() const
{
Matrix<U> m;
// ...
return m;
}
// Explicit instantiation.
template class Matrix<int>;
template class Matrix<float>;
template Matrix<float> Matrix<int>::ReinterpretAs<float>();
template Matrix<int> Matrix<float>::ReinterpretAs<int>();
上面的最后两行给出了编译器警告:
warning #536: no instance of function template "Matrix<T>::ReinterpretAs
[with T=float]" matches the specified type
提前谢谢你,马克
【问题讨论】:
标签: c++ templates visual-c++