Jeremy W
Jeremy W

Reputation: 23

Variadic Template lambda expansion

I'm trying to implement a delegate class following Herb Sutter's Example. There is a sections in this article that duplicates several templates; one template for the number of arguments in the list (Example 7, lines 41 - 59)1. I'm trying to replace this with a variadic template.

void operator()() const {
    for_each(begin(l_), end(l_), []( function<F> i) {
            i(); 
    });
}

template<typename... Ts>
    void operator()(Ts... vs) const {
        for_each(begin(l_), end(l_), [&, vs...]( function<F> i) //g++-4.6.1: expected ',' before '...' token; expected identifier before '...' token
        { 
                i(vs...);
        });
    }

I found this answer, but I think my issue is the vs isn't expanding. What is the correct way to do this?

Upvotes: 2

Views: 1314

Answers (2)

pmr
pmr

Reputation: 59811

This seems to be an old gcc bug that still persists. Might want to give the maintainers a friendly nudge.

A workaround could be:

#include <vector>
#include <algorithm>
#include <functional>
#include <tuple>

using namespace std;

template<typename F>
struct Foo {
  template<typename... Ts>
  void operator()(Ts... vs) const {
    auto t = tie(vs...);

    for_each(begin(l_), end(l_), 
             [&t]( function<F> i) 
             { 
               // figure out the function arity and unpack the tuple
               // onto it. This is could be more difficult than one
               // thinks.

               // i(vs...);
             }
      );
  }
  vector< function< F > > l_;
};

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

I'm not sure why vs isn't expanding but maybe the easy work-around is to pass arguments by value as default and name the known objects needing pass by reference. This way the expansion in the capture-list isn't needed. Personally, I wouldn't be surprised if this were a bug in gcc (your example is incomplete - otherwise I'd try it with a recent SVN versions of gcc and clang).

Upvotes: 2

Related Questions