c++ - How to use default template parameters in partial template specialization with variadic templates and multiple parameter packs -
c++ - How to use default template parameters in partial template specialization with variadic templates and multiple parameter packs -
i have problem specialization of template class using 2 different kinds of variadic parameter packs. in detail, have "variadic type" like
template< typename... arguments > struct variadictype{};
the template class using single type , 2 different variadic parameter packs is
template< typename type , class , class > struct foo{};
we specialize template gaining access 2 different parameter packs follows:
template< typename type , template< class... > class firstpack_container , class... first , template< class... > class secondpack_container , class... sec > struct foo< type , firstpack_container< first... > , secondpack_container< second... > > { // foo(first... first , second... second){ std::cout << "sizeof...(first) = " << sizeof...(first) << std::endl; std::cout << "sizeof...(second) = " << sizeof...(second) << std::endl; } };
for instantiation of object of type foo have typedef 2 different variadictype's:
typedef variadictype<int,float,double> firstpack; typedef variadictype<double,std::string> secondpack;
with types in hand may write
foo<int,firstpack,secondpack> my_foo_object(1,2.,3.,4.,"bla");
but if want utilize foo-structure 1 variadic parameter pack, have write:
typedef variadictype<> emptytype; foo<int,firstpack,emptytype> my_bar_object(1,2.,3.);
therefore question arises, if there possibility or workaraound give specialized foo construction emptytype default template parameter secondpack_container. alternatively, possible specialize foo 1 time again 1 variadic parameter pack , if yes, how?
for sake of simplicity, here working minimal cpp-file compilation (using gcc 4.7):
#include<string> #include<iostream> // compile debug-information : g++ -o x.bin -wall -wextra -pedantic -std=c++11 -g x.cpp // holder-class variadic arguments template< typename... arguments > struct variadictype{}; // template class usage of 2 different variadic parameter-packs template< typename type , class , class > struct foo{}; // specialization of foo direct access of 2 different variadic parameter-packs template< typename type , template< class... > class firstpack_container , class... first , template< class... > class secondpack_container , class... sec > struct foo< type , firstpack_container< first... > , secondpack_container< second... > > { // foo(first... first , second... second){ std::cout << "sizeof...(first) = " << sizeof...(first) << std::endl; std::cout << "sizeof...(second) = " << sizeof...(second) << std::endl; } }; int main(){ // definition of "variadic pack types" usage in foo: typedef variadictype<int,float,double> firstpack; typedef variadictype<double,std::string> secondpack; // // instantiation of foo-object 2 parameter-packs: foo<int,firstpack,secondpack> my_foo_object(1,2.,3.,4.,"bla"); // // now: sec parameter pack has empty. // "variadic empty type": typedef variadictype<> emptytype; // instantiation of foo-object firstpack: foo<int,firstpack,emptytype> my_bar_object(1,2.,3.); homecoming 0; }
edit
sorry, questions not concise! specialization of struct foo 1 variadic parameter pack obvious , simple. want avoid plant whole construction foo farther time specialization. did next simple trick: created variadic templatetypefactory follows:
typedef variadictype<> emptytype; template<typename type,class x1,class x2=emptytype> struct templatetypefactory{}; template< typename returntype, template<class...> class firsttype , class... first , template<class...> class secondtype , class... sec > struct templatetypefactory<returntype,firsttype<first...>,secondtype<second...>>{ typedef foo<returntype,variadictype<first...>,variadictype<second...>> type; }; template< typename returntype, template<class...> class firsttype , class... first > struct templatetypefactory<returntype,firsttype<first...>,empty_type>{ typedef foo<returntype,variadictype<first...>,empty_type> type; };
note: in deklaration of templatefactory used empttype default-parameter sec template type x2.
to utilize templatetype mill may write:
typedef templatetypefactory<int,firstpack,secondpack> foo_type_3; foo_type_2::type foo_3(1,2.,3.,4.,"bla"); typedef templatetypefactory<int,firstpack> foo_type_4; foo_type_4::type foo_4(1,2.,3.);
therefore question answered myself!
c++ templates specialization variadic default-parameters
Comments
Post a Comment