c++ - Reducing number of template arguments for class -
i have method , 2 classes defined this:
template<template<class x> class t> void dosomething() { t<int> x; } template <class t> class classwithonearg { t t; }; template <class t1, class t2> class classwithtwoargs { t1 t1; t2 t2; };
i can
dosomething<classwithonearg>();
but cannot
dosomething<classwithtwoargs>();
however, i'd pass classwithtwoargs dosomething, t2 = double.
the method found create
template <class t1> class classwithtwoargs_child : public classwithtwoargs<t1, double> { };
and then
dosomething<classwithtwoargs_child>();
this works, in concrete case classes require constructor argument , have create constructor argument in _child-class , pass base want avoid.
do have idea how that?
thanks lot!
indirection solution. instead of template template parameter pass "meta function" -- function maps 1 type in form of struct nested class template:
struct mf1 { template<class arg1> struct eval { typedef classtemplatewithonearg<arg1> type; }; }; template<class arg2> struct mf2 { template<class arg1> struct eval { typedef classtemplatewithtwoargs<arg1,arg2> type; }; }; template<class metafunc> void do_something() { typedef typename metafunc::template eval<int>::type clazztype; clazztype x; } void foo() { do_something<mf1>(); do_something<mf2<double> >(); }
in c++0x reduced "template typedef":
template<class arg1> using newclasstemplate = classtemplatewithtwoargs<arg1,double>;
which allows pass newclasstemplate
template template argument accepts 1 template parameter.
Comments
Post a Comment