Reputation: 1330
Is there any simple method for accessing inner members of whole parameter pack? Lets say you have following code
#include <iostream>
class A {
typedef int type;
constexpr static type C = 5;
};
class B {
typedef unsigned type;
constexpr static type C = 6;
};
template <class T1, class ... TList>
std::ostream & printer(std::ostream & out, T1 t1, TList ... plist) {
out << t1 << " ";
return printer(out, plist...);
}
template <class T1>
std::ostream & printer(std::ostream & out, T1 t1) {
return out << t1 << std::endl;
}
template <class ... TList>
std::ostream & proxy(std::ostream & out, TList ... plist) {
return printer(out, std::forward<TList::type ...>(plist::C ...));
}
int main(int argc, const char * argv[])
{
proxy(std::cout, A(), B());
return 0;
}
I want proxy function to unpack member variables of plist and pass them to printer. Is there a simple way how to do this without iterating through parameter list?
Upvotes: 1
Views: 527
Reputation: 24606
After clearing several issues with your code, i could make it compile:
A
and B
should be structs to make their definitions public.printer
before the variadic oneproxy now looks like this:
template <class ... TList>
std::ostream & proxy(std::ostream & out, TList ... plist) {
//leave the forward, plist is a variable pack, so operator.
return printer(out, plist.C ...);
//or:
return printer(out, TList::C...);
}
And since the C
's are static constexpr members, you can just leave the argument pack:
template <class ... TList>
std::ostream & proxy(std::ostream & out) {
return printer(out, TList::C ...);
}
int main(int argc, const char * argv[])
{
proxy<A,B>(std::cout);
return 0;
}
FYI, the correct call to std::forward
would have looked like this, if C
was an ordinary member variable of A and B:
template <class ... TList>
std::ostream & proxy(std::ostream & out, TList&& ... plist) {
return printer(out, std::forward<typename TList::type>(plist.C)...);
}
Upvotes: 2