Is it possible to call a parameter of a type of a variational pattern of a static method?

Suppose we have classes:

class A { public: static void m() {} } class B { public: static void m() {} } template<typename... T> class C { public: void c() { T::m(); // Call somehow m() of A and B, if T is a parameter pack of A and B } } 

How can I extend a parameter package and call a static method for each type?

+5
source share
1 answer

The problem is that we cannot simply expand the parameter package and call it inside the function body, because this is not a valid context.

 void c() { T::m()...; //invalid context for parameter pack expansion } 

There are many tricks to get around this. The one I usually use uses std::initializer_list :

 void c() { (void)std::initializer_list<int> { (T::m(), 0)... }; //valid context } 

Demo

In C ++ 17, we get fold expressions , which will greatly simplify the situation:

 void c() { (T::m(), ...); //much nicer } 
+5
source

All Articles