不同类型模板化类的显式模板化函数实例化
C++ Explicit templated function instantiation for templated class of different type
我试图在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
提前感谢Mark
你错过了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++ - 使用另一个类的构造函数实例化一个对象
- 与参数匹配的友元模板函数实例化
- 在保证复制的世界中构造函数实例化
- SFINAE 和模板函数实例化:为什么在启用了 SFINAE 类型的函数参数中使用模板参数时无法推断模板参数?
- 防止复制构造函数实例化 C++11 类"deleting"
- 如何键入定义一个专门的 std::set 模板,使用特定的比较函数实例化
- 使用不同类型的模板函数实例化
- C 调用模板构造函数实例化
- 为什么很清楚模板函数实例化不会内联
- 模板函数实例化文件
- 模板函数实例化的可移植性问题
- 模板函数实例化 自定义数据类型的问题
- 模板类成员函数实例化
- C++.对象使用错误的构造函数实例化
- C++如何用参数化构造函数实例化对象
- 为什么无法使用复制构造函数实例化"non const"而可以在没有复制构造函数的情况下实例化配对?
- 双模板化函数实例化失败
- 延迟 crtp 基类中的成员函数实例化
- 如何根据容器的元素类型启用模板函数实例化