Is it possible to “store” a template parameter pack without expanding it?

后端 未结 4 1511
醉话见心
醉话见心 2020-11-27 11:01

I was experimenting with C++0x variadic templates when I stumbled upon this issue:

template < typename ...Args >
struct identities
{
    typedef Args t         


        
4条回答
  •  天涯浪人
    2020-11-27 11:39

    This is a variation of GManNickG's neat partial specialization trick. No delegation, and you get more type safety by requiring the use of your variadic_typedef struct.

    #include 
    
    template
    struct variadic_typedef {};
    
    template
    struct convert_in_tuple {
        //Leaving this empty will cause the compiler
        //to complain if you try to access a "type" member.
        //You may also be able to do something like:
        //static_assert(std::is_same<>::value, "blah")
        //if you know something about the types.
    };
    
    template
    struct convert_in_tuple< variadic_typedef > {
        //use Args normally
        typedef std::tuple type;
    };
    
    typedef variadic_typedef myTypes;
    typedef convert_in_tuple::type int_float_tuple; //compiles
    //typedef convert_in_tuple::type int_float_tuple; //doesn't compile
    
    int main() {}
    

提交回复
热议问题