views:

71

answers:

1

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.

+4  A: 

This will not work because Entity<Args>::InnerEntity is a non-deduced context. Means that ArgsA... and ArgsAInner... cannot be deduced, likewise for the other parameter. This is because before the compiler can deduce Args, it has to know what type InnerEntity is a member of, but to know that, it has to deduce Args.

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.

template<typename ...Args>
class Entity {
  template<typename ...ArgsInner>
  class InnerEntity {

  };

  template<typename ...ArgsAInner, typename... ArgsBInner>
  > friend auto my_func(
        InnerEntity<ArgsAInner...> A
      , InnerEntity<ArgsBInner...> B
  ) -> tuple<decltype(A), decltype(B)> {
      return make_tuple(A, B);
  }

};

You could also declare some member typedef in InnerEntity that specifies the type of the outer class, and formulate my_func in terms of that, so that SFINAE can sort it out for non-members.

template<typename ...Args>
class Entity {
  template<typename ...ArgsInner>
  class InnerEntity {
    typedef Entity outer_entity;
  };    
};

template<typename A, typename B, typename Result>
struct require_entity { };

template<typename ...ArgsA, typename ...ArgsB, typename Result>
struct require_entity<Entity<ArgsA...>, Entity<ArgsB...>> {
   typedef Result type;
};

template<template<typename...> class AInner, template<typename...> class BInner, 
         typename ...ArgsAInner, typename ...ArgsBInner>
> auto my_func(
      AInner<ArgsAInner...> A
    , BInner<ArgsBInner...> B
) -> typename require_entity<
         typename AInner<ArgsAInner...>::outer_entity, 
         typename BInner<ArgsBInner...>::outer_entity, 
           tuple<decltype(A), decltype(B)>>::type 
{
    return make_tuple(A, B);
}

Of course you don't need that template<typename...> class AInner thing if you don't need to access the ArgsAInner types, like in the above my_func. In such a case you are better off just accepting typename AInner and have less to write. The SFINAE will still make sure only the right thing is accepted.

Johannes Schaub - litb
@Johannes I see. Perhaps I'm leaving pertinent info out here, I'm still a little confused on why the above wouldn't work. Entity has some sub-classes that define what Args would be, and it has a member function for creating InnerEntity objects, and that member function accepts and passes along the args for InnerEntity as a pack. Given that, shouldn't the compiler be able to deduce the various Args packs, since they'd all be known at compile-time?
pheadbaq
@pheadbaq `Entity` could be specialized for some arguments such that `InnerEntity` is a completely different thing than for other arguments. In general for a function template function parameter if you have `typename T::foo`, then `T` cannot be deduced by that parameter.
Johannes Schaub - litb
@Johannes After chewing on your answer and my own code a bit more, I don't think the friend solution will work after all, because ThingA and ThingB (see my edited code) do not inherit from the same Entity template. Looks like the typename AInner + SFINAE would work fine though. Thanks!
pheadbaq