How to differentiate template classes.
Hi group,
I hope this text looks okay. Unfortunately, I am currently
forced to use google groups.
I have a question regarding differentiating templated classes.
Assume I have (the real code is more complex):
template< .... > class my_template;
typedef my_template<double,double> lat_long;
typedef my_template<double,double> complex;
I still have only one type, so I could input a complex to
a function expecting a complex - reducing typesafety.
What is perhaps worse, I can't overload on the two types.
std::ostream operator<< must be the same for both types.
One simple solution would be to simply inherit, but this
seems a little like abusing the system. E.g.:
struct complex: public my_template<double,double>
{
// very little boilerplate to construct
};
What does the group say about this? Should I worry?
My inheritance-kludge gives me another problem. My
template class is part of a group of similar templates,
(they all represent more or less simple types such as
integers, floats, array, structs and unions)
and I must be able to give them some common functionality:
template< ... > template_t2;
template< ... > template_t3;
template< ... > template_t4;
template<typename T>
void func(T const& t)
{
// Using functionality common to all my templated
// functions so this should be safe.
if (!T::fixed_size && t.peru_size() << 4000) .../////
}
// But one of my templates need a special treatment
template<typename T...>
void func(template_t2<T...> const& );
But I guess the compiler would then choose the generic type
for func. How do I flexibly fix that problem? It would be nice
if my users would be allowed to create their own compatible
types (such as a std::map-like structure).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]