Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:30:01+00:00 2026-05-17T00:30:01+00:00

I’m trying to make a function template that will accept two (or more) of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:30:02+00:00Added an answer on May 17, 2026 at 12:30 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.