如何获得专用模板来使用成员函数的非专用版本?
考虑以下代码:
template <int dim>
struct vec
{
vec normalize();
};
template <>
struct vec<3>
{
vec cross_product(const vec& second);
vec normalize();
};
template <int dim>
vec<dim> vec<dim>::normalize()
{
// code to normalize vector here
return *this;
}
int main()
{
vec<3> direction;
direction.normalize();
}
编译此代码会产生以下错误:
1>main.obj:错误 LNK2019:无法解析的外部符号“public: struct vec<3> __thiscall vec<3>::normalize(void)” (?normalize@?$vec@$02@@QAE?AU1@ XZ) 在函数 _main 中引用
Consider the following code:
template <int dim>
struct vec
{
vec normalize();
};
template <>
struct vec<3>
{
vec cross_product(const vec& second);
vec normalize();
};
template <int dim>
vec<dim> vec<dim>::normalize()
{
// code to normalize vector here
return *this;
}
int main()
{
vec<3> direction;
direction.normalize();
}
Compiling this code produces the following error:
1>main.obj : error LNK2019: unresolved external symbol "public: struct vec<3> __thiscall vec<3>::normalize(void)" (?normalize@?$vec@$02@@QAE?AU1@XZ) referenced in function _main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能:)你想要的是专门化成员函数:
另一个稍微复杂的解决方案是使用boost::enable_if:
如果调用cross_product,这将导致编译时错误any dim != 3。请注意,“trick”仅适用于带参数的函数,因为只有这样才能自动推导模板参数。 对于没有参数的情况,我在上面提供了一个函数
without_parameters
:)。You can't :) What you want is to specialize the member functions instead:
Another, slightly more complicated solution is to use
boost::enable_if
:That will cause a compile time error if cross_product is called for any dim != 3. Note that that 'trick' only works for functions with parameters, since only then the template parameter can be auto-deduced. For cases without parameters, i have provided a function
without_parameters
above :).您尚未提供 vec<3>::normalize 的定义,因此链接器显然无法链接到它。
模板专业化的全部要点是您可以提供每种方法的专业化版本。 除非在这种情况下你实际上并没有这样做。
You haven't supplied a definition of vec<3>::normalize, so the linker obviously can't link to it.
The entire point in a template specialization is that you can supply specialized versions of each method. Except you don't actually do that in this case.
据我所知,你不能称之为“通用”版本。
或者,您可以将类外部的通用实现定义为函数:
You can't as far as I know call the "generic" version.
Alternatively, you can define your generic implementations outside of the classes as functions: