简体   繁体   English

C ++显式模板化函数实例化,用于不同类型的模板化类

[英]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.

相关问题 C++ 中模板化 class 的模板化友元的显式模板实例化 - Explicit template instantiation of templated friend of templated class in C++ 模板化类中模板化函数的显式实例化 - explicit instantiation of a templated function in the templated class C ++模板化类定义 - 不同类的模板化返回类型 - C++ Templated Classes Definition - Templated Return Type of Different Class 模板类的模板构造函数的C ++显式模板专业化 - C++ explicit template specialization of templated constructor of templated class 模板类的模板化构造函数的显式实例化 - Explicit instantiation of templated constructor for template class 非模板类的显式类实例化 - Explicit class instantiation for non-templated classes 使用模板化成员函数显式实例化模板类 - Explicit instantiation of template class with templated member functions 模板化 function 参数的显式模板实例化 - Explicit template instantiation for a templated function parameter C ++:在模板化类中存在命名成员时,在模板化类中提供类函数? - C++: providing a class function in templated class on existence of named member in its templated type? C ++模板化类,其行为取决于类型 - C++ templated class with different behaviour depending on type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM