Topic: Comment wanted on chg
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/07/19 Raw View
In article <3trf09$ess@citadel.evolving.com>,
Todd Blanchard <tblancha@evolving.com> wrote:
>When working with classes that use lots of template mixins, I have run across
>the following:
>
>The old way:
>
>template <class T> class A { public: A<T>& func(const T&); };
>class B { };
>class C { };
>class D : public A<B>, public A<C> { };
>
>This doesn't work because:
>
>C++ looks for any function called func in the current and all base classes.
>If it finds more than one, it refuses to compile because
>overloading across base classes is currently not allowed.
>I would propose a change that would allow overloading across template
>base classes of the same class but parameterized by different types as long
>as such functions can be resolved without ambiguity.
>Assuming no one talks me out of it (ie shows me this is bad) I intend to
>submit this proposal first of next week.
I don't like this. I do not think it can fly. It makes
more sense to simply allow overloading across bases. Placing
an artificial boundary between "template classes" and others
is a BAD idea. Consider:
template<class T> struct X { void f(X const&); };
struct A {};
struct B {};
struct XA : X<A> {};
struct XB : X<B> {};
struct D : X<A>, X<B> {}; // special rule
struct DD : XA, XB {}; // WHO KNOWS?
In the last case the direct bases are NOT template classes,
so the overloading across bases doesn't apply .. but
there are two _indirect_ bases here which ARE template classes.
Does the rule apply?
We need C++ to be SIMPLER not more complicated.
The current idiom requires something like:
struct D : X<A>, X<B> {
using X<A>::func;
using X<B>::func;
};
D d;
A a;
d.func(a); // overloaded and resolved
The use of the using declaration here is intended to allow
overloading across bases. (it is a little problematic so don't
count on it quite yet)
BTW: quite a lot of C++ idioms require lots of boring
slavish mechanical code repetition. You can always
write a code generator -- in C++ of course !
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: tblancha@evolving.com (Todd Blanchard)
Date: 1995/07/10 Raw View
When working with classes that use lots of template mixins, I have run across
the following:
The old way:
template <class T> class A { public: A<T>& func(const T&); };
class B { };
class C { };
class D : public A<B>, public A<C> { };
This doesn't work because:
C++ looks for any function called func in the current and all base classes.
If it finds more than one, it refuses to compile because
overloading across base classes is currently not allowed. As it is now,
a function must be found by name in a single base class, then overload
resolution based on arguments is performed. If the name cannot be resolved
unambiguously before overload resulution, the compiler signals error.
The new way:
I would propose a change that would allow overloading across template
base classes of the same class but parameterized by different types as long
as such functions can be resolved without ambiguity. This would facilitate
a much more transparent mixin style of programming as in:
B b;
C c;
D d;
d->func(c); // calls A<C>::func
d->func(b); // calls A<B>::func
Assuming no one talks me out of it (ie shows me this is bad) I intend to
submit this proposal first of next week.
Todd Blanchard