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 6050867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:44:18+00:00 2026-05-23T07:44:18+00:00

Short question: Can I typedef a variadic argument pack? I need template <typename …T>

  • 0

Short question: Can I typedef a variadic argument pack? I need template <typename ...T> struct Forward { typedef T... args; };.


Long version:

I was thinking about reimplementing the excellent boost bimap in C++0x. Recall that a bimap of two types S and T is a std::set of relations between S x and T y. The objects themselves are stored in two independent internal containers, and the relations track the associated iterators I suppose; both types can serve as keys via “left” and “right” lookup. Depending on the choice of internal containers, values may be unique or not, e.g. if the left container is a set and the right container is a multiset, then one x can map to many different ys, and right lookup gives an equal-range. Popular internal containers are set, multiset, vector and list, and maybe the unordered_* versions too.

So we need a type which accepts two containers as template parameters:

class Bimap<S, T, std::set, std::multiset>

But we must accept that the containers can take arbitrary many arguments, so we need to pass all those, too. If we just needed one set of variadic arguments, it wouldn’t be a problem, since we could pass those directly. But now we need two sets of arguments, so I want to write a forwarder, to be used like so:

Bimap<int, int, std::set, std::set, Forward<std::less<int>, MyAllocator>, Forward<std::greater<int>, YourAllocator>> x;

Here’s the template I came up with:

#include <set>
#include <cstdint>

template <typename ...Args>
struct Forward
{
  typedef Args... args; // Problem here!!
  static const std::size_t size = sizeof...(Args);
};

template <typename S, typename T,
          template <typename ...SArgs> class SCont,
          template <typename ...TArgs> class TCont,
          typename SForward = Forward<>, typename TForward = Forward<>>
class Bimap
{
  typedef SCont<S, typename SForward::args> left_type;
  typedef TCont<T, typename TForward::args> right_type;

  template <typename LeftIt, typename RightIt> struct Relation; // to be implemented

  typedef Relation<typename left_type::const_iterator, typename right_type::const_iterator> relation_type;

};


int main()
{
  Bimap<int, int, std::set, std::set, Forward<std::less<int>>, Forward<std::greater<int>>> x;
}

Unfortunately, in the indicated line in Forward I cannot figure out how to typedef the parameter pack! (The commented line gives a compiler error.)

[I suppose I could go for a lazy version Bimap<std::set<int, MyPred>, std::multiset<char, YourPred>> x; and extract the types via LeftCont::value_type and RightCont::value_type, but I thought it’d be nicer if I could make the key types my primary template arguments and allow defaulting to std::set containers.]

  • 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-23T07:44:19+00:00Added an answer on May 23, 2026 at 7:44 am

    You can achieve what you want by encapsulating the variadic argument pack in a tuple and later using the following two helper template structs to forward the actual variadic arguments:

    template<typename PackR, typename PackL>
    struct cat;
    
    template<typename ...R, typename ...L>
    struct cat<std::tuple<R...>, std::tuple<L...>>
    {
            typedef std::tuple<R..., L...> type;
    };
    

    and

    template<typename Pack, template<typename ...T> class Receiver>
    struct Unpack;
    
    template<typename ...Args, template<typename ...T> class Receiver>
    struct Unpack<std::tuple<Args...>, Receiver>
    {
            typedef Receiver<Args...> type;
    };
    

    your code sample would look like this:

    #include <set>
    #include <cstdint>
    #include <tuple>
    
    template<typename PackR, typename PackL>
    struct Cat;
    
    template<typename ...R, typename ...L>
    struct Cat<std::tuple<R...>, std::tuple<L...>>
    {
            typedef std::tuple<R..., L...> type;
    };
    
    template<typename Pack, template<typename ...T> class Receiver>
    struct Unpack;
    
    template<typename ...Args, template<typename ...T> class Receiver>
    struct Unpack<std::tuple<Args...>, Receiver>
    {
            typedef Receiver<Args...> type;
    };
    
    template<typename ...Args>
    struct Forward
    {    
            //typedef Args... args; // Problem here!!
            typedef std::tuple<Args...> args; // Workaround
    
            static const std::size_t size = sizeof...(Args);
    };
    
    template<typename S, typename T, 
            template<typename ...SArgs> class SCont, 
            template<typename ...TArgs> class TCont, 
            typename SForward = Forward<> ,
            typename TForward = Forward<>>
    class Bimap
    {
            //typedef SCont<S, typename SForward::args> left_type;
            //typedef TCont<T, typename TForward::args> right_type;
            typedef typename Unpack<typename Cat<std::tuple<S>, typename SForward::args>::type, SCont>::type left_type; //Workaround
            typedef typename Unpack<typename Cat<std::tuple<T>, typename TForward::args>::type, TCont>::type right_type; //Workaround
    
            template<typename LeftIt, typename RightIt> struct Relation; // to be implemented
    
            typedef Relation<typename left_type::const_iterator, typename right_type::const_iterator> relation_type;
    
    };
    
    int main()
    {
        Bimap<int, int, std::set, std::set, Forward<std::less<int>> , Forward<std::greater<int>>> x;
    }
    

    which compiles just fine under gcc 4.6.0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long story below short question: How can I get Spring's NamedParameterJDBCTemplate join Hibernate's session?
A short question: I've got a TabNavigator with multiple canvas children. How can i
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
The short version of the question - why can't I do this? I'm restricted
Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
Short question is: how can i use graph api oauth redirects mechanism to authenticate
The short question is - how can I prevent (delay) a bound UI element
Hi! The short question is: what can be the problem? The overall memory usage
Short question: How can I modify individual items in a List ? (or more
I have a very short question: How can i fetch mysql content using php

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.