【问题标题】:C++ Explicit templated function instantiation for templated class of different type不同类型的模板类的 C++ 显式模板函数实例化
【发布时间】: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++


    【解决方案1】:

    你错过了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;
    

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多