 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mirek Fidler Guest
|
Posted: Mon Jul 14, 2003 5:41 pm Post subject: template class with template base class confusion... |
|
|
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>: = 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 ]
|
|
| Back to top |
|
 |
Richard Smith Guest
|
Posted: Tue Jul 15, 2003 11:50 pm Post subject: Re: template class with template base class confusion... |
|
|
Mirek Fidler wrote:
| Quote: | template <class T
struct Base {
T x;
};
template
struct Derived : public Base
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>: = 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 ]
|
|
| Back to top |
|
 |
Emil Dotchevski Guest
|
Posted: Fri Jul 18, 2003 2:41 am Post subject: Re: template class with template base class confusion... |
|
|
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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|