21

I have a function template like this:

template <class ...A>
do_something()
{
  // i'd like to do something to each A::var, where var has static storage
}

I can't use Boost.MPL. Can you please show how to do this without recursion?

EDIT: These days (c++17), I'd do it like this:

template <class ...A>
do_something()
{
  ((std::cout << A::var << std::endl), ...);
};
user1095108
  • 14,119
  • 9
  • 58
  • 116
  • 2
    [This Q&A](http://stackoverflow.com/questions/14261183/how-to-make-generic-computations-over-heterogeneous-argument-packs-of-a-variadic) should help. – Andy Prowl Mar 19 '13 at 13:50
  • 1
    I don't have any arguments in my function, the referenced solution has arguments. – user1095108 Mar 19 '13 at 13:56
  • Who cares if there are arguments? This is about expanding variadics, works the same. – Marc Glisse Mar 19 '13 at 14:05
  • 4
    Generally when dealing with variadics: Create a context where pack expansion can be used, isolate what you want to do into a function `func`, and expand the pack as `func(expr_containing_pack)...` in said context. – Xeo Mar 19 '13 at 14:11

3 Answers3

17

What Xeo said. To create a context for pack expansion I used the argument list of a function that does nothing (dummy):

#include <iostream>
#include <initializer_list>

template<class...A>
void dummy(A&&...)
{
}

template <class ...A>
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<S1,S2,S3>();
    std::cout << S1::var << S2::var << S3::var;
}

This program prints 111.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
  • My solution: `[](...){ }((A::var = 1)...);`. Change yours maybe and I accept? Actually, it's Xeo's... – user1095108 Mar 19 '13 at 14:23
  • @user1095108 Nice one, added. – jrok Mar 19 '13 at 14:29
  • @user1095108 Altough, you ought to be careful with ellipsis, it's undefined behavior to pass them a non-POD type. I'm not sure if it applies in this case, but still. – jrok Mar 19 '13 at 14:35
  • 3
    In that case, I suppose, one could add a comma and 0, i.e. `[](...){ }((A::var = 1, 0)...);`, or am I talking rubbish? – user1095108 Mar 19 '13 at 14:39
  • 2
    This is similar to one of my suggestions. It has one issue: the order in which the elements are processed is not specified by the standard. For this example GCC, for instance, process the elements in this order S3::var, S2::var and S1::var. – Cassio Neri Mar 19 '13 at 14:56
  • 1
    I generally use `struct swallow{ template swallow(T&&...){} };` for this, with `swallow{(A::var = 1)...}`, which enforces left-to-right order. – Xeo Mar 19 '13 at 14:57
  • @Xeo Your swallow trick did not work, when I tested it. I think you need an initializer list constructor to make it work. – user1095108 Jun 28 '13 at 20:45
  • Does this preserve the order of argument evaluation? This is imporant for e.g. a variadic template `print` function. – rubenvb Jul 20 '13 at 10:41
  • @user1095108: That's probably your compiler. The standard mandates left-to-right evaluation for any braced-init-list. As a workaround, or alternative way, `using swallow = int[];` and then `swallow{0, ((A::var = 1), 0)...};`. – Xeo Jul 20 '13 at 11:21
  • @Xeo the workaround in my case was to initialize a `std::initializer_list`, i.e. `std::initializer_list{ ((A::var = 1), 0)... };` Why did you put the zero before the `((A::var = 1)` in your comment? – user1095108 Jul 20 '13 at 12:11
  • 1
    @user1095108: If the pack is empty, you'd get a zero-sized array, which is ill-formed. – Xeo Jul 20 '13 at 12:14
  • Thank! Is it possible to use a complex code (e.g. some non-static function / something like inside a lambda-function-with-capturing ) in place of `A::var = 1`? – cppBeginner Dec 05 '17 at 09:36
6

As an example, suppose you want to display each A::var. I see three ways to acomplish this as the code below illustrates.

Regarding option 2, notice that the order in which the elements are processed is not specified by the standard.

#include <iostream>
#include <initializer_list>

template <int i>
struct Int {
    static const int var = i;
};

template <typename T>
void do_something(std::initializer_list<T> list) {
    for (auto i : list)
        std::cout << i << std::endl;
}

template <class... A>
void expand(A&&...) {
}

template <class... A>
void do_something() {

    // 1st option:
    do_something({ A::var... });

    // 2nd option:
    expand((std::cout << A::var << std::endl)...);

    // 3rd option:
    {
        int x[] = { (std::cout << A::var << std::endl, 0)... };
        (void) x;
    }
}

int main() {
    do_something<Int<1>, Int<2>, Int<3>>();
}
Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
1

The answers above work -- here I explore a bit more on using a lambda for complex use cases.

Lambda 101: [ capture ]( params ){ code }( args to call "in-place" );

If you want to expand a lambda with a variadic template, it won't work as mentioned above when the parameters is of a non-trivial type:
error: cannot pass object of non-trivial type 'Foo' through variadic method; call will abort at runtime.

The way to go is to move the code from the lambda's args to code:

template <class ...A>
do_something() {
  Foo foo;
  [&](auto&& ...var){
    (foo.DoSomething(var), ...);
  }(A::var...);
}
user1095108
  • 14,119
  • 9
  • 58
  • 116
Rudolf Real
  • 1,948
  • 23
  • 27
  • lol, yeah, my question is pretty old, you can even do better in c++20 and make the lambda accept `...A`. So, no need for an extra function. I fixed a small error in your answer. – user1095108 Oct 25 '21 at 19:35