Re: accessing members of a templated base class
On Sun, 10 Aug 2003 14:13:59 +0100, John Harrison wrote:
[color=blue]
> This code fails to compile on Comeau C++ and VC++ 7.1 (with language
> extensions disabled)
>
> template <class T>
> struct B
> {
> T b;
> };
>
> template <class T>
> struct D : B<T>
> {
> void f() { b = 1; }
> };
>
> int main()
> {
> D<int> x;
> x.f();
> }
>
> Error messages refer to 'b = 1;' with the message 'undefined identifier b'.
> Substituting this->b for b or making B a non-template class both make the
> code compile.
>
> What's going on here? I was so surprised when I saw this on VC++ that I
> assumed it was a compiler bug, but apparently not.
>
> john[/color]
It's definitely not a compiler bug. HP's aCC compiler even tells you where
to look in the Standard:
Error (future) 641: "xx.cc", line 16 # Undeclared variable 'b'. A variable
with the same name exists in a template base class, but is not visible
according to the Standard lookup rules (See [temp.dep], 14.6.2(3) in the
C++ Standard). You can make it visible by writing 'this->b'.
void f() { b = 3; }
^
There is an explanation of this rule in the book "C++ Templates - The
Complete Guide" by Vandevoorde and Josuttis (section 9.4.2).
On Sun, 10 Aug 2003 14:13:59 +0100, John Harrison wrote:
[color=blue]
> This code fails to compile on Comeau C++ and VC++ 7.1 (with language
> extensions disabled)
>
> template <class T>
> struct B
> {
> T b;
> };
>
> template <class T>
> struct D : B<T>
> {
> void f() { b = 1; }
> };
>
> int main()
> {
> D<int> x;
> x.f();
> }
>
> Error messages refer to 'b = 1;' with the message 'undefined identifier b'.
> Substituting this->b for b or making B a non-template class both make the
> code compile.
>
> What's going on here? I was so surprised when I saw this on VC++ that I
> assumed it was a compiler bug, but apparently not.
>
> john[/color]
It's definitely not a compiler bug. HP's aCC compiler even tells you where
to look in the Standard:
Error (future) 641: "xx.cc", line 16 # Undeclared variable 'b'. A variable
with the same name exists in a template base class, but is not visible
according to the Standard lookup rules (See [temp.dep], 14.6.2(3) in the
C++ Standard). You can make it visible by writing 'this->b'.
void f() { b = 3; }
^
There is an explanation of this rule in the book "C++ Templates - The
Complete Guide" by Vandevoorde and Josuttis (section 9.4.2).
Comment