Simple parameter pack expansion: expected ';'

884 Views Asked by At

In the following snippet:

void normalize(path& p)
{
   // do something with p
}

template<typename... T>
void normalize(T&... t)
{
     normalize(t)...;  // (L)
}

In my actual understanding, the line (L) expands to:

template<typename... T>
void normalize(T&... t)  // T = {t1, t2, t3}, for example
{
    normalize(t1), normalize(t2), normalize(t3);
}

and each one of these expressions will execute the one-parameter version of normalize. However, g++ (4.8.1) throws me the following error:

prueba.cpp: In function 'void normalize(T& ...)':
prueba.cpp:155:17: error: expected ';' before '...' token
     normalize(t)...;
             ^
prueba.cpp:155:20: error: parameter packs not expanded with '...':
     normalize(t)...;
                    ^
prueba.cpp:155:20: note:         't'

What's wrong with my code?

2

There are 2 best solutions below

6
On BEST ANSWER

The expansion of parameter packs is not allowed in this context. If you create a helper class pass, you can do the following:

pass{(normalize(std::forward<T>(t)), 0)...};

The helper class pass might look as follows:

struct pass
{
    template <typename ...Args>
    explicit pass(Args&&...) { }
};
1
On

I think you would want to unpack your parameter pack into a function call site or tuple, like this:

  make_tuple(normalize(t)...);  // (L)

For example, the following code would compile under gcc-4.8.1:

#include <tuple>
using namespace std;
typedef  int path;
void normalize(path& p)
{
   // do something with p
}

template<typename... T>
void normalize(T&... t)
{
  auto tp = make_tuple(normalize(t)...);  // (L)
  //do whatever you want with the components of tp ...
}

main() {}