如何根据类模板参数专门化成员函数
问题说了什么。另外,是否可以内联执行此操作?
这是一个小例子,只是为了提供一个想法......
template<typename T>
class Foo {
public:
Foo() :z(0.0) {}
void do( const Foo<T> &f ) {
z = f.z;
}
// specialize 'do' for Foo<int>, possible inline?
private:
T z;
};
What the question says. In addition, is it possible to do this inline?
Here is a small example just to give an idea...
template<typename T>
class Foo {
public:
Foo() :z(0.0) {}
void do( const Foo<T> &f ) {
z = f.z;
}
// specialize 'do' for Foo<int>, possible inline?
private:
T z;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不需要做任何复杂的事情。只需使用重载和委托即可。请注意,我们不能只添加一个
int
重载,因为当T
结果也是int
时,这将是一个无效的重载(两个函数 。或者,对于这种情况,您可以通过专门化来做到这
一点 只有当所有模板参数都固定时,才可以使用这种专门化方式 换句话说,部分专门化成员函数是不可能的。
You don't need to do anything complicated. Just use overloading and delegation. Note that we cannot just add an
int
overload, because whenT
turns out to beint
too, this would be an invalid overload (two functions with the same signature)Or, for this case, you can do this by specialization
Using specialization this way is only possible if all template arguments are fixed. In other words, partially specializing the member function is not possible.
您可以通过使成员函数成为成员函数模板并使用 SFINAE(替换失败不是错误)来获得此行为。例如:
is_integral
类型特征测试U
是否是整数类型。如果不是,则实例化第一个;如果是,则实例化第二个。is_same
类型特征测试可确保T
和U
是同一类型。这用于确保不会为Foo
之外的任何类型实例化成员函数模板。此示例使用 C++0x
库; Boost 还有类型特征库 您可以使用它,其工作原理基本相同。You can sort of get this behavior by making the member function a member function template and using SFINAE (substitution failure is not an error). For example:
The
is_integral
type trait test whetherU
is an integer type. If it is not, the first is instantiated; if it is, the second is instantiated.The
is_same
type trait tests to ensureT
andU
are the same type. This is used to ensure that the member function template is not instantiated for any type other thanFoo<T>
.This example makes use of the C++0x
<type_traits>
library; Boost also has a type traits library that you can use, which works mostly the same.您可以尝试做这样的事情(没有测试,可能不起作用):
You may try to do something like this (didn't test, might not work):