调用C++中对象列表的对象方法

chr*_*irk 2 c++ function object

如何为每个提供给函数的对象设计一种方法来调用对象方法?

也就是说,

ResetAll(obj1, obj2, obj3, ...)
Run Code Online (Sandbox Code Playgroud)

会叫obj1.Reset(),obj2.Reset()等...

对象不在List或任何其他STL容器中.

Ker*_* SB 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)...);
}
Run Code Online (Sandbox Code Playgroud)

用法:

ResetAll(ob1, obj2, some_obj, another_obj);
Run Code Online (Sandbox Code Playgroud)