[英]C++ Explicit templated function instantiation for templated class of different type
I am trying to explicitly instantiate a templated function of type U inside a templated class of type T. My code below generates a warning and the linker does not find the explicit instantiation of ReinterpretAs()
. 我试图在类型为T的模板化类中显式实例化类型U的模板化函数。下面的代码生成警告,链接器找不到
ReinterpretAs()
的显式实例化。 Can anyone spot the error or advise on how to do this? 任何人都可以发现错误或建议如何做到这一点? I am using VC++ 2010.
我正在使用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>();
The last two lines above give a compiler warning: 上面的最后两行给出了编译器警告:
warning #536: no instance of function template "Matrix<T>::ReinterpretAs
[with T=float]" matches the specified type
Thank you in advance, Mark 马克,提前谢谢你
You're missing the const
. 你错过了
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.