more typelists & templates


My co-worker Aaron was quick to reply to my earlier post on templates in type-lists with this alternative:



You instead add a non-templated function invoker object:


 


struct factory_invoker{


    template <class a, class b>


    struct apply{


        typedef typedef factory<a,b>::type type;


    };


};


 


Then your typelist has normal types in the list, but those types happen to be template metafunctions.  To invoke one of these that is in a typelist:


 


// assuming tlist1 is a typelist


typename tlist1::head::template apply<int, char>::type


 


(the above ‘template’ and ‘typename’ keywords are, as always, only required or even allowd when used inside a template)


Which in this case will end up naming the type ‘MyType<int, char>’ for you.


This also works. However in my case since I was trying to reduce the number of generated types, so I stuck with the previous solution.


Thanks Aaron!


Comments (1)

  1. aaronla says:

    Also note that the following definition of factory_invoker is also valid and more concise, provided that your factory returns types by the typedef ‘type’

       struct factory_invoker{

           template <class a, class b>

           struct apply : factory<a,b>{ };

       };

    The typedef is only strictly necessary when adapting template metafunction return type names.

    #aaron

Skip to main content