Connecting Tech Pros Worldwide Help | Site Map

Inherited member name qualification for templated class

Lionel B
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Inherited member name qualification for templated class


Lionel B wrote:[color=blue]
> [...]
> 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?[/color]

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

V
Lionel B
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Inherited member name qualification for templated class


Victor Bazarov wrote:[color=blue]
> Lionel B wrote:[color=green]
> > [...]
> > 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?[/color]
>
> Have you tried using 'this->'? The pun is not intended.[/color]

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

Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Inherited member name qualification for templated class


Lionel B wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>>Lionel B wrote:
>>[color=darkred]
>>>[...]
>>>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?[/color]
>>
>>Have you tried using 'this->'? The pun is not intended.[/color]
>
>
> 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?
>[/color]

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
Lionel B
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Inherited member name qualification for templated class


Victor Bazarov wrote:[color=blue]
> Lionel B wrote:[color=green]
> > Victor Bazarov wrote:
> >[color=darkred]
> >>>[...][/color][/color]
>
> 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.[/color]

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

Closed Thread