Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 461
Mark Avatar asked Jan 18 '23 04:01

Mark


1 Answers

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;
like image 112
Pubby Avatar answered Jan 22 '23 05:01

Pubby