Variadic typedefs, or “Bimaps done the C++0x way”

后端 未结 2 1876
花落未央
花落未央 2021-01-05 04:20

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


Long version

2条回答
  •  醉梦人生
    2021-01-05 05:09

    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
    struct cat;
    
    template
    struct cat, std::tuple>
    {
            typedef std::tuple type;
    };
    

    and

    template class Receiver>
    struct Unpack;
    
    template class Receiver>
    struct Unpack, Receiver>
    {
            typedef Receiver type;
    };
    

    your code sample would look like this:

    #include 
    #include 
    #include 
    
    template
    struct Cat;
    
    template
    struct Cat, std::tuple>
    {
            typedef std::tuple type;
    };
    
    template class Receiver>
    struct Unpack;
    
    template class Receiver>
    struct Unpack, Receiver>
    {
            typedef Receiver type;
    };
    
    template
    struct Forward
    {    
            //typedef Args... args; // Problem here!!
            typedef std::tuple args; // Workaround
    
            static const std::size_t size = sizeof...(Args);
    };
    
    template class SCont, 
            template class TCont, 
            typename SForward = Forward<> ,
            typename TForward = Forward<>>
    class Bimap
    {
            //typedef SCont left_type;
            //typedef TCont right_type;
            typedef typename Unpack, typename SForward::args>::type, SCont>::type left_type; //Workaround
            typedef typename Unpack, typename TForward::args>::type, TCont>::type right_type; //Workaround
    
            template struct Relation; // to be implemented
    
            typedef Relation relation_type;
    
    };
    
    int main()
    {
        Bimap> , Forward>> x;
    }
    

    which compiles just fine under gcc 4.6.0

提交回复
热议问题