从指向成员的指针推导类模板
作为类模板参数之一,我需要使用指向成员的指针:
template <class Base, typename Member, Member Base::*m>
class MemPtrTestUgly
{
...
};
这需要用作
struct S
{
int t;
}
MembPtrTestUgly <S, int, &S::t> m;
但我想这样使用它:
MemPtrTestNice<S, &S::t> m;
成员类型是从成员指针推导出来的。我无法使用函数模板,因为 MemPtrTest
类不应该被实例化(只有一些静态函数将被使用)。有没有办法在纯 C++03 中做到这一点(没有 Boost 或 TR1)?
As one of the class template parameters I need to use a pointer to member:
template <class Base, typename Member, Member Base::*m>
class MemPtrTestUgly
{
...
};
This needs to be used as
struct S
{
int t;
}
MembPtrTestUgly <S, int, &S::t> m;
But I want to use it as this:
MemPtrTestNice<S, &S::t> m;
The member type is deduced from the member pointer. I cannot use function template, as the MemPtrTest
class is not supposed to be instantiated (there are just some static functions that will be used). Is there a way how to do it in pure C++03 (no Boost or TR1)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用部分专业化并获得一个非常漂亮的实现:
这将用作:
当然,如果您不想隐式指定成员类型,则这需要
decltype
或等效项。You can use partial specialization and get a pretty nice-looking implementation:
This would be used as:
Of course, this requires
decltype
or an equivalent, if you don't want to implicitly specify the member type.