By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Inherited member name qualification for templated class

P: n/a
Greetings,

The following code:

<code>

template<typename T>
class A
{
protected:
T n;
};

template<typename T>
class B : public A<T>
{
public:
void foo()
{
n = 1; // <--------- error
}
};

int main()
{
B<int> b;
b.foo();

return 0;
}

</code>

generates the error:

test.cpp: In member function `void B<T>::foo()':
test.cpp:15: error: `n' undeclared (first use this function)

If the base class member n is qualified with the full templated base
class name, as in:

A<T>::n = 1; // <--------- ok

it compiles fine. Also, for /non/ templated classes it is not necessary
to qualify the base class member name.

Could the compiler not infer from the templated class declarations that
the n in foo() refers to A<T>::n ?

Problem is, that in my real-world code there are (at least) four
template parameters and *many* inherited base class members; having to
quote the full templated base class name for every reference to an
inherited base class member would make my code incredibly clunky and
utterly unreadable. Is there a workaround for this?

Regards,

--
Lionel B

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least) four
template parameters and *many* inherited base class members; having to
quote the full templated base class name for every reference to an
inherited base class member would make my code incredibly clunky and
utterly unreadable. Is there a workaround for this?


Have you tried using 'this->'? The pun is not intended.

V
Jul 23 '05 #2

P: n/a
Victor Bazarov wrote:
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least)
four template parameters and *many* inherited base class
members; having to quote the full templated base class name
for every reference to an inherited base class member would
make my code incredibly clunky and utterly unreadable. Is
there a workaround for this?


Have you tried using 'this->'? The pun is not intended.


Yes that does work... but it then leaves my code littered with
"this->"es. Not as bad as base_class<foo_type, bar_type, baz_type,
....>::myvar all over the place, perhaps, but still pretty ugly.

I am still a bit puzzled as to why the compiler can't seem to resolve
the name. Can't think of a situation where such a construct might be
ambiguous - and even if there is one, my case is surely unambiguous?

--
Lionel B

Jul 23 '05 #3

P: n/a
Lionel B wrote:
Victor Bazarov wrote:
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least)
four template parameters and *many* inherited base class
members; having to quote the full templated base class name
for every reference to an inherited base class member would
make my code incredibly clunky and utterly unreadable. Is
there a workaround for this?


Have you tried using 'this->'? The pun is not intended.

Yes that does work... but it then leaves my code littered with
"this->"es. Not as bad as base_class<foo_type, bar_type, baz_type,
...>::myvar all over the place, perhaps, but still pretty ugly.

I am still a bit puzzled as to why the compiler can't seem to resolve
the name. Can't think of a situation where such a construct might be
ambiguous - and even if there is one, my case is surely unambiguous?


This has been discussed many times. The base classes are not searched
during name lookup if the base classes are dependent. It is often the
case with templates. See "dependent name lookup" for more information.

V
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
Lionel B wrote:
Victor Bazarov wrote:
[...]


This has been discussed many times. The base classes are not
searched during name lookup if the base classes are dependent.
It is often the case with templates. See "dependent name
lookup" for more information.


Thanks, it's there in the FAQ (35.12 - didn't know what to look for -
and yes, it did make my head hurt). Seems like a feasible solution to
my situation might be a "using" statement:

template<typename T>
class B : public A<T>
{
using A<T>::n;
public:
void foo()
{
n = 1;
}
};

Cheers,

--
Lionel B

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.