成员函数traits [英] Member function traits
问题描述
我编写一个模板类来封装成员函数以减少一些调用 - 如果一些条件为真,则不需要调用成员函数。签名将类似这样
I am writing a template class that wraps around member functions to reduce some calls - if some condition is true, the member function doesn't need to be called. The signature would look something like this
template <typename MemFuncType, MemFuncType> class MemberWrapper;
我可以专门研究:
template <typename R, typename T, R T::* MemFunc> class MemberWrapper<R T::*, MemFunc>{};
我也想限制 RT :: *
。
我可以想到的唯一解决方案是通过提供基于返回类型,函数类型,参数列表的部分特化来实现成员函数traits类和cv限定符。这将导致一个繁琐的实现,如当前 std :: mem_fn
overloads 。是否有更好的方法?
The only solution I can think of is to implement a member functions traits class by providing partial specializations based on return type, function type, arguments list and cv-qualifiers. This would lead to a cumbersome implementation like the current std::mem_fn
overloads. Is there a way of doing it better?
编辑:更改 Ret
到 / code>。
EDIT : Changed Ret
to R
. As pointed out in the comments, it isn't really the return type and the specialization was invalid.
推荐答案
Boost函数类型提供了一个伟大的函数traits集合。此外,此 SO post 显示示例。
Boost Function Types provides a great collection of function traits. Also, this SO post shows examples.
这篇关于成员函数traits的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!