Topic: template class with template base class confusion...


Author: cxl@volny.cz ("Mirek Fidler")
Date: Mon, 14 Jul 2003 17:41:37 +0000 (UTC)
Raw View
template <class T>
struct Base {
   T x;
};

template <class T>
struct Derived : public Base<T> {
   Derived() { x = 10; }
};

does not compile with Comeau C++.

Anyway, by introducing dependant name

template <class T>
struct Derived : public Base<T> {
   Derived() { Base<T>::x = 10; }
};

everything works OK.

I am little bit confused about things standard says about this
situation. Can anybody explain please ?

Mirek


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com (Richard Smith)
Date: Tue, 15 Jul 2003 23:50:16 +0000 (UTC)
Raw View
Mirek Fidler wrote:

> template <class T>
> struct Base {
>    T x;
> };
>
> template <class T>
> struct Derived : public Base<T> {
>    Derived() { x = 10; }
> };

How does the compiler know what x is?  Until the template is
instantiated for some particular T, specialisations of
Base<T> may be introduced, perhaps which do not have a
member called x.  To resolve this problem, the compiler does
not look up x in the scope of Base<T> -- if a global
variable x exists, it will be used, otherwise the compiler
will issue a diagnostic.

There are two ways of forcing it to look up x in the scope
of the base class:

  this->x = 10;

and

  Base<T>::x = 10;

I personally prefer the former syntax.

There's a very good explanation of this in Vandevoorde and
Josuttis excellent book "C++ Templates: The Complete Guide",
section 9.4.2.  If you frequently write template code, I
strongly suggest you get a copy of this book.

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: emild@collectivestudios.com (Emil Dotchevski)
Date: Fri, 18 Jul 2003 02:41:10 +0000 (UTC)
Raw View
There is an even simpler example that does not compile under Comeau:

  template <class T>
  class Foo: T {
    Foo(){x=0;}
  };

  "ComeauTest.c", line 4: error: identifier "x" is undefined

MSVC .NET reports no errors, but it is supposed to. The correct code is:

  template <class T>
  class Foo: T
  {
    Foo() { this->x=0; }
  };

D&E goes into details why the standard is the way it is.

--Emil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]