如何为每个提供给函数的对象调用一个对象方法?
即,
ResetAll(obj1, obj2, obj3, ...)
将致电obj1.Reset()
,obj2.Reset()
等...
对象不在List或任何其他STL容器中。
答案 0 :(得分:5)
也许是一个可变参数模板:
template <typename ...Objs> struct Resetter;
template <typename Obj, typename ...Rest> struct Resetter<Obj, Rest>
{
static inline void reset(Obj && obj, Rest &&... rest)
{
std::forward<Obj>(obj).Reset();
Resetter<Rest...>::reset(std::forward<Rest>(rest)...);
}
};
template <> struct Resetter<> { static inline void reset() { }; };
// Type-deducing helper
template <typename ...Objs> inline void ResetAll(Objs &&... objs)
{
Resetter<Objs...>::Reset(std::forward<Objs>(objs)...);
}
用法:
ResetAll(ob1, obj2, some_obj, another_obj);
答案 1 :(得分:2)
概念证明,因为编写可疑的C ++代码很有趣:
#include <vector>
#include <iostream>
using namespace std;
#define Invoke(A, T,M,...)\
do{\
struct _{ \
_& operator,(T* value){ \
value->M A ; \
return *this; \
} \
} _; _, __VA_ARGS__; \
}while(0)
#define m_a_all(...) Invoke( (), Obj,m_a,__VA_ARGS__)
#define m_b_all(...) Invoke( (), Obj,m_b,__VA_ARGS__)
#define m_c_all(A, ...) Invoke(A, Obj,m_c,__VA_ARGS__)
class Obj{
public:
virtual void m_a(){
cout << "Obj::m_a " << this << endl;
}
virtual void m_b(){
cout << "Obj::m_b " << this << endl;
}
virtual void m_c(int a, double b){
cout << "Obj::m_c(" << a <<"," << b<< ") " << this << endl;
}
};
class Derived : public Obj{
public:
virtual void m_a(){
cout << "Derived::m_a " << this << endl;
}
virtual void m_b(){
cout << "Derived::m_b " << this << endl;
}
virtual void m_c(int a, double b){
cout << "Derived::m_c(" << a <<"," << b<< ") " << this << endl;
}
};
int main(){
Obj *o1 = new Obj(), *o2 = new Obj(), *o3 = new Derived();
m_a_all(o1, o2, o3);
cout<<endl;
m_a_all(o1);
cout<<endl;
m_b_all(o1, o2, o3);
cout<<endl;
m_c_all( (3,42.0), o1, o2, o3);
return 0;
}
示例输出:
Obj::m_a 0x9e0a008
Obj::m_a 0x9e0a018
Derived::m_a 0x9e0a028
Obj::m_a 0x9e0a008
Obj::m_b 0x9e0a008
Obj::m_b 0x9e0a018
Derived::m_b 0x9e0a028
Obj::m_c(3,42) 0x9e0a008
Obj::m_c(3,42) 0x9e0a018
Derived::m_c(3,42) 0x9e0a028
链式operator,
重载允许使用__VA_ARGS__
。
一个限制是只有在所有参数共享一个基本类型时才能使用它,为其定义了要调用的方法。
这应该具有最小的开销,并且不依赖于任何库。
答案 2 :(得分:1)
如果您可以使用Boost.Preprocessor接受一些繁重的预处理程序滥用行为:
#include <boost/preprocessor.hpp>
#define CALL_RESET(z, n, data) BOOST_PP_CAT(p, n).Reset();
#define GENERATE_VARIADIC_FUNCTION(z, n, data) \
template <BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(n), typename T)> \
void ResetAll(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(n), T, & p)) \
{ \
BOOST_PP_CAT(BOOST_PP_REPEAT_, z)(BOOST_PP_INC(n), CALL_RESET, ) \
}
/* ^ this generates the function
template <typename T0, typename T1, typename T2, ...>
void ResetAll(T0& p0, T1& p1, T2& p2, ...)
{
p0.Reset(); p1.Reset(); p2.Reset(); ...
}
*/
BOOST_PP_REPEAT(BOOST_PP_LIMIT_REPEAT, GENERATE_VARIADIC_FUNCTION, )
// ^ and this creates all of the above up to 256.
例如,
#include <cstdio>
struct Obj1 { void Reset() { printf("1\n"); } };
struct Obj2 { void Reset() { printf("2\n"); } };
struct Obj3 { void Reset() { printf("3\n"); } };
int main()
{
Obj1 o1;
Obj2 o2;
Obj3 o3;
ResetAll(o1, o2, o3, o2, o1, o3);
return 0;
}
将在屏幕上打印'1 2 3 2 1 3'(证明:http://ideone.com/FRNvJ)。
(顺便说一句,如果其中一个输入是右值,这将产生一个不可逾越的错误消息墙。)