Re: Template template partial specialization
On 3/5/2011 11:48 AM, Hizo wrote:
I would like to use partial template specialization with template
parameters and optional template parameters, but I didn't find out
documentation about it and my compiler (g++) doesn't seem to accept
it : so how to proceed ?
Here it is :
-----------------------
#include<functional>
using std::equal_to;
using std::less_equal;
typedef int AType;
/* a class */
template< class T>
class Object
{
public:
Object(AType a, T t)
{
/* init */
}
/* class code */
};
/* abstract class (designated as an interface for a relation order
between vectors) */
template< class>
class VectorWeakOrder
{
/* code */
};
/* template class (no polymorphism but template because we must create
pointer of that type order for further use of special properties of it
in one of the methods of this class) */
template< template<class> class VectorWeakOrderT, class ValueType,
class Equal = equal_to< ValueType> >
class ROR
{
/* code */
typedef Object< VectorWeakOrderT<ValueType> > ObjT;
ObjT create() const
{
AType a;
/* code */
return ObjT( a, VectorWeakOrderT<ValueType>() );
}
};
/* template SpecialOrder class which is a VectorWeakOrder of type T */
template< class T, class Equal = equal_to<T>, class LessOrEqual =
less_equal<T> >
class SpecialOrder: VectorWeakOrder<T>
{
/* class code */
};
/* partially specializing class ? */
template< class ValueType, class Equal = equal_to< ValueType>, class
LessOrEqual = less_equal< ValueType> >
class ROR< SpecialOrder<class T,Equal,LessOrEqual>, ValueType, Equal>
I'm still unclear (a) why you think you need this and (b) how you'd use
that class template ROR. If the first argument of this template is
unused in the case of the specialization, then why do you need it to be
specific?
I have a sneaky suspicion, that you need a particular 'create' member
for all 'ROR' that have SpecialOrder with specific second and third
argument as its first argument, but I have hard time convincing myself
that there is no other solution...
{
/* same code as before (before I was trying to specialize only a
templated method of the unspecialized template class but I read that
it was not standard so I gave up this idea and specialized the entire
class which was templated with another argument which is only useful
for this method) */
Does it have to be a method of the class? Couldn't it be a stand-alone
function? You could always use overloading (since there is no partial
specializing of function templates)...
/* specialized method */
ObjT create() const
{
AType a;
SpecialOrder< ValueType, Equal, LessOrEqual> special_order_obj;
Since you're using a full specialization here, the only reason to go
into all this trouble is because you need to supply something as the
first argument for this template class partial specialization, yes?
/* do some stuff with order_obj and the class members (const) */
return ObjT( a, special_order_obj );
}
};
-------------------------------------
As for partial specializations, do you have a copy of Vandevoorde and
Josuttis, "C++ Templates"? It might help you immensely. There are
probably numerous examples of specializations you can already find in
previous posts, and generally on the Web. Have you tried googling for them?
No I have not, but that's why I thought I could find help here.
We will provide as much help as we can, but a newsgroup advice is by no
means a replacement for proper studying with a proper book.
However I searched around in the web but I didn't find out examples of
template template parameter partial specialization, or the partial
specialization wasn't on the template template parameter but on other
parameters.
Thanks for your time.
V
--
I do not respond to top-posted replies, please don't ask