简体   繁体   中英

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() . 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

Thank you in advance, Mark

You're missing the 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM