Topic: idea: typedef in baseclass list -- and 2 questions
Author: "Gabor Greif" <gabor@no.netopia.com>
Date: 1999/12/20 Raw View
I have come across this several times:
Consider some templated class with many template args:
template <typename A, typename B, typename C>
struct Base
{
Base(A&, B, C*);
};
template <typename A, typename B, typename C>
struct Derived : Base<Derived<A, B, C>, B, std::pair<A, C> >
{
typedef Base<Derived<A, B, C>, B, std::pair<A, C> > Inherited;
Derived(B b, std::pair<A, C>* c) : Inherited(*this, b, c) { }
};
This a contrived example, but you get the idea. The problem is that in the
design phase you will shuffle around those template params quite a bit.
Updating that typedef is rather tedious. This could be avoided if typedefs
were allowed in the baseclass list:
template <typename A, typename B, typename C>
struct Derived : typedef Base<Derived<A, B, C>, B, std::pair<A, C> >
Inherited
{
Derived(B b, std::pair<A, C>* c) : Inherited(*this, b, c) { }
};
The semantics could be defined by translation to standard C++ this way
(appropriately extended to the MI case):
class <classname> : <access> typedef <base> <name>
{
...
};
---->
class <classname> : <access> <base>
{
<access>: typedef <base> <name>;
<default-access>:
...
};
Any thoughts? I would love to name the base classes this way.
####################
A related question: In my Derived definition I pass Derived<A, B, C> to the
base template as a parameter. I recognize that I can use Derived instead of
Derived<A, B, C> as an alias inside of the class definition. But can I use
this alias in the baseclass list too?
To wit:
Is
template <typename A, typename B, typename C>
struct Derived : Base<Derived, B, std::pair<A, C> >
{
typedef Base<Derived, B, std::pair<A, C> > Inherited;
Derived(B b, std::pair<A, C>* c) : Inherited(*this, b, c) { }
};
legal std c++?
####################
Another related question:
Typedef allows the aliasing of typenames. This is very useful for giving
names to type applications in a certain scope. Now with the appearance of
member templates and template template parameters I see the need for
aliasing template names too. One could extend typedef to cover this
problem. Any better ideas?
Gabor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]