C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template class with template base class confusion...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Mirek Fidler
Guest





PostPosted: Mon Jul 14, 2003 5:41 pm    Post subject: template class with template base class confusion... Reply with quote



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>:Mad = 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





PostPosted: Tue Jul 15, 2003 11:50 pm    Post subject: Re: template class with template base class confusion... Reply with quote



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>:Mad = 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





PostPosted: Fri Jul 18, 2003 2:41 am    Post subject: Re: template class with template base class confusion... Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.