Nondependent Base Classes in templates

M

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?

Thanks!
 
J

Juan Antonio Zaratiegui Vallecillo

muler escribió:
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?


You must specify the whole type, form the point of view os the derived
class:

typename Base<T>::T strange_;

Best regards,

Zara
 
A

Alf P. Steinbach

* 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
 
M

muler

muler escribió:








You must specify the whole type, form the point of view os the derived
class:

       typename Base<T>::T strange_;

Best regards,

Zara

thanks zara! wow, how come i forgot that?!
 
M

muler

muler escribió:








You must specify the whole type, form the point of view os the derived
class:

       typename Base<T>::T strange_;

Best regards,

Zara

dear Zara,

by default the "T strange_" in class Derived refers to Base<T>::T. My
question was how can I instead refer to the template parameter T of
class Derived.

thanks!
 
M

muler

* muler:








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) ?
 
A

Alf P. Steinbach

* muler:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top