我正在尝试在 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
提前谢谢你,马克
最佳答案
您缺少 const
。
template class Matrix<int>;
template class Matrix<float>;
template Matrix<float> Matrix<int>::ReinterpretAs<float>() const;
template Matrix<int> Matrix<float>::ReinterpretAs<int>() const;
关于C++ 不同类型模板化类的显式模板化函数实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302407/