2

I apologize, but I had a big problem in expressing the case in one sentence of the title.

The problem: I have a class template based on many "properties" (don't take this "property" keyword too literally):

template<typename ... P>
class C: public P... {}

Then I want separate, free function template that takes two instances and performs some call for each "property":

template<typename ... P>
void foo(C<P> &c1, C<P> &c2)
{
    { doSomething<p>()... }      // for each type p in pack P
}

template<typename T>
void doSomething() {}

The problem is that P doesn't represent a set of my function arguments. Instead it parametrizes argument types. So I don't know how to write "expansion". Is such thing possible at all?

ardabro
  • 1,907
  • 16
  • 31
  • 1
    I'm not clear on what you're trying to do. Could you show an example of how you want to use this? – cigien Apr 16 '20 at 21:31
  • Does this answer your question? [recursive variadic template to print out the contents of a parameter pack](https://stackoverflow.com/questions/7124969/recursive-variadic-template-to-print-out-the-contents-of-a-parameter-pack). This should at least give you an idea of something you could do. –  Apr 16 '20 at 21:35

1 Answers1

4

It seems to me that you are just missing the parameter pack expansion in the function parameters. Then there is also a small typo (p->P) and you seem to intend to use a fold-expression to execute the function for all types which has a slightly different syntax:

template<typename ... P>
void foo(C<P...> &c1, C<P...> &c2)
{
    // Parentheses and comma required to signify fold expression using the comma operator
    ( doSomething<P>(), ... ); 
}

I also feel like you would want to pass c1 and c2 on to doSomething (which probably should be taking he objects as T&, I guess?), otherwise that function will depend only on the type, not an actual object of that type.

walnut
  • 21,629
  • 4
  • 23
  • 59