Re: Nondependent Base Classes in templates
* muler:
On Oct 26, 11:23 am, "Alf P. Steinbach" <al...@start.no> wrote:
* muler:
Hi,
Consider this:
template <class X>
class Base {
public:
typedef double T;
};
template <class T>
class Derived : Base<int> { // nondependent base
public:
T strange_; // T is Base<int>::T not template parameter T
};
Question: how can I refer to template parameter T in the derived class
without: 1) changing the typedef in Base and 2) changing the name of
template parameter T in Derived to sth else?
I'm not sure whether the code above is valid but I think it is, that
Base<int>::T shadows the template parameter T.
If you have the option of changing Base then do so; single-letter names are bad
except where idiomatic, and a main reason is the name conflict possibility.
Otherwise just choose your option (2).
Cheers & hth.,
- Alf
So, does that mean there is no way except to change the name of
template parameter T to sth else (that is option 2) ?
No. There is a third way. But it's only of academic interest (it's something you
should never do in actual code, so called "clever" stuff), and so if you
absolutely want that then one must assume that this is Homework -- because
going for an impractical application of some piece of knowledge, to sort of
single that out as the only answer, is one hallmark of Homework, and a question
that contains its own answer, like, how long did the 6-day war last or what
color was Napoleon's white horse, is another such hallmark of Homework.
Cheers & hth.,
- Alf