开发者

Accepting nested variadic class templates as arguments to function template

开发者 https://www.devze.com 2023-01-17 09:14 出处:网络
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 diff

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开发者_JAVA技巧 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>::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.

0

精彩评论

暂无评论...
验证码 换一张
取 消