C++ 不同类型模板化类的显式模板化函数实例化 [英] C++ Explicit templated function instantiation for templated class of different type

查看:42
本文介绍了C++ 不同类型模板化类的显式模板化函数实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在类型 T 的模板化类中显式实例化 U 类型的模板化函数.我下面的代码生成一个警告,并且链接器没有找到 ReinterpretAs() 的显式实例化.任何人都可以发现错误或建议如何执行此操作吗?我使用的是 VC++ 2010.

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(). Can anyone spot the error or advise on how to do this? I am using 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

先谢谢你,马克

推荐答案

您缺少 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++ 不同类型模板化类的显式模板化函数实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆