Inherited member name qualification for templated class

L

Lionel B

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,
 
V

Victor Bazarov

Lionel said:
[...]
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
 
L

Lionel B

Victor said:
Lionel said:
[...]
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?
 
V

Victor Bazarov

Lionel said:
Victor said:
Lionel said:
[...]
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
 
L

Lionel B

Victor said:
Lionel said:

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,
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top