I have a template c开发者_如何学编程lass ,MyClass<class E,class T>
,and i want to use :
std::unary_function<T,bool> _predicate
;
std::binary_function<T,E,void> _swaper
;
as template template parameters in my class , notice the bool and the void which is partial template specialization ....
can someone tell how MyClass
decleration and ctor that should take predicate and swaper as arguments should look like ?
Thanks!
depends if you need to use the parameters only in the constructor or not. Could be:
class MyClass
{
template <class T, class E>
MyClass(std::unary_function<T,bool> p, std::binary_function<T,E,void> s)
{
...
}
...
}
or
template <class T, class E>
class MyClass
{
MyClass(std::unary_function<T,bool> p, std::binary_function<T,E,void> s)
{
...
}
...
}
i've just figured it out ... there is no use of ctor like:
MyClass(std::unary_function<T,bool> p, std::binary_function<T,E,void> s)
the std::unary_function is just a struct decleration it does not contain operator() and ofcourse not a virtual operator() , thus it has no use as an argument in a function...
this is how it supposed to look like :
template <class T, class E,class PredicateFunctionUnaryOperator,class SwapFunctionBinaryOperator>
class MyClass
{
private:
PredicateFunctionUnaryOperator _p;
SwapFunctionBinaryOperator _s;
T _arg1;
E _arg2;
public:
MyClass(PredicateFunctionUnaryOperator p, SwapFunctionBinaryOperator s,T arg1,E arg2): _p(p),_s(s)
{
_arg1 = arg1;
_arg2 = arg2;
}
void f()
{
std::cout<<"Unary Function output is :"<<_p(_arg1)<<endl;;
std::cout<<"Binary Function Output is:"<<_s(_arg1,_arg2);
std::cout<<"Chauoo!!"<<endl;
}
};
but as i can see its working i cant understand when is the special syntax template decleration that is related with "template template parameters" is used...
Thanks.
精彩评论