I have a function template like this:
template
do_something()
{
// i\'d like to do something to each A::var, where var has static storag
What Xeo said. To create a context for pack expansion I used the argument list of a function that does nothing (dummy):
#include
#include
template
void dummy(A&&...)
{
}
template
void do_something()
{
dummy( (A::var = 1)... ); // set each var to 1
// alternatively, we can use a lambda:
[](...){ }((A::var = 1)...);
// or std::initializer list, with guaranteed left-to-right
// order of evaluation and associated side effects
auto list = {(A::var = 1)...};
}
struct S1 { static int var; }; int S1::var = 0;
struct S2 { static int var; }; int S2::var = 0;
struct S3 { static int var; }; int S3::var = 0;
int main()
{
do_something();
std::cout << S1::var << S2::var << S3::var;
}
This program prints 111.