Re: Are homonymous non-template and template classes incompatible?
Hi Sylvain,
I agree it is a pitty we can't overload over typenames in the same way
we use do with functions. I wanted to do that for exactly the same
reason (i.e. providing concrete representation for an abstract base
class without introducing more "names") and I don't see why this is not
supported by the compiler (I guess the standard doesn't say anything
about this.) Does anyone knows the answer?
I think it is something that happens before the name demangler even
runs.
My workaround would be to declare the class with a default dummy type
and implement it as the base abstract class. The resulting syntax is
not exactly what you want but it is quite close.
template<typename T=bool> class my_class; //bool is the "dummy" type
template<>
struct my_class<bool>{
....
}
template<typename T> struct my_class: my_class<bool>{
....
}
my_class<> my_abstract_object; // <-- almost the desired syntax, just
"<>" added.
my_class<double> my_concrete_object;
Corrections, comments and improvements are more than welcomed!
Please I would like to know if this "idiom"/workaround is in use by
anyone or if anybody invented it before.
Thanks, Alfredo Correa
struct my_class // Interface
{
virtual void do_it()=0;
virtual ~my_class() {}
};
template<typename T> struct my_class: my_class // Implementation
{
void do_it() {}
};
my_class<int> my_obj; // Object instanciation
G++ is definitely confused by this code:
g++ -c template.cpp -o /dev/null
template.cpp:7: error: `my_class' is not a template type
template.cpp:8: error: recursive type `my_class' undefined
template.cpp:8: error: redefinition of `struct my_class'
template.cpp:2: error: previous definition of `struct my_class'
template.cpp:12: error: non-template type `my_class' used as a
template
template.cpp:12: error: ISO C++ forbids declaration of `my_obj' with
no type
template.cpp:11: confused by earlier errors, bailing out
Is this a limitation of the compiler or of the C++ language? My
opinion
is that if the compiler parser was able to cope with the syntax, then
the generated code would be supported by any linker because of the
different mangling of non-template and template homonymous classes.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]