I’m trying to make a function template that will accept two (or more) of the nested variadic class templates listed below, as arguments, and put them into another data structure that will accept different types (pair or tuple is what I’ll most likely use). Here are the classes and subclasses, along with the usage of my function (the function is defined farther below):
template<typename... Args> struct Entity {
template<typename... InnerEntArgs> struct InnerEntity {
InnerEntity(InnerEntArgs... inner_ent_args) {
... //do stuff w/ InnerEntArgs pack
... //do stuff that makes Inner dependent on Outer's Args pack
}
};
};
struct ThingA : Entity<int, string> {
... //construct ThingA
};
struct ThingB : Entity<string, string> {
... //construct ThingB
};
auto foo = my_func(
ThingA::InnerEntity<int, int, int>(1, 2, 3)
, ThingB::InnerEntity<string, int>("bar", 1)
);
Below is the code I cobbled together for the function, and it does compile fine, but I’m not sure if it is set up correctly. Specifically, I’m a little fuzzy on how typename and ::template are making the compiler happy in this context, or if this function will behave the way I’m expecting:
template<
typename... ArgsA, typename... ArgsAInner
, typename... ArgsB, typename... ArgsBInner
> auto my_func(
typename Entity<ArgsA...>::template InnerEntity<ArgsAInner...> A
, typename Entity<ArgsB...>::template InnerEntity<ArgsBInner...> B
) -> tuple<decltype(A), decltype(B)> {
return make_tuple(A, B);
}
I think I have a good grasp on how the parameter packs are being deduced/inferred, and how auto, decltype, and the trailing return type are doing their thing, but if I’m mistaken, please let me know how.
Also, if anyone cares to demonstrate a variadic version of this function that can accept any number of the nested variadic class templates and put them into a suitable container or data structure, that’d be great, but I’m primarily concerned with fully understanding typename and ::template. Thanks ahead of time!
*If I’ve worded this title incorrectly or I’m mixing up terms, please explain. 🙂 I’m here to learn.
This will not work because
Entity<Args>::InnerEntityis a non-deduced context. Means thatArgsA...andArgsAInner...cannot be deduced, likewise for the other parameter. This is because before the compiler can deduceArgs, it has to know what typeInnerEntityis a member of, but to know that, it has to deduceArgs.You can put this function as a friend function template into
Entity<Args...>and make it work as long as both are members of the same template. But last time I checked, GCC did not find friend functions defined in class templates.You could also declare some member typedef in
InnerEntitythat specifies the type of the outer class, and formulatemy_funcin terms of that, so that SFINAE can sort it out for non-members.Of course you don’t need that
template<typename...> class AInnerthing if you don’t need to access theArgsAInnertypes, like in the abovemy_func. In such a case you are better off just acceptingtypename AInnerand have less to write. The SFINAE will still make sure only the right thing is accepted.