Topic: Scope of inherited data members in templated classes
Author: simguy@cox.net (Bryan P Walsh)
Date: Sun, 13 Oct 2002 16:18:33 +0000 (UTC) Raw View
Hi All,
I was recently porting some code to a new linux compiler and was
suprised when the following code failed to compile:
template <class T>
class A
{
public:
A() : m_somedata(5) {}
~A() {}
protected:
T m_somedata;
};
template <class T>
class B : public A<T>
{
public:
B() : m_moredata(2) {}
~B() {}
T getData() {return m_somedata + m_moredata;}
protected:
T m_moredata;
};
According to the vendor, the correct form for getData would be:
T getData() {return A<T>::m_somedata + m_moredata;}
The vendor references the following C++ standard issue.
http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/cwg_defects.html#213
The new form seems counter-intuitive me and I was suprised since the
first form of getData works properly on all of the other compilers I've
tried. Is the vendor interpreting the standard correctly? Is the first
form really illegal?
Thanks,
Bryan Walsh
---
[ 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: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Sun, 13 Oct 2002 20:34:41 +0000 (UTC) Raw View
On Sun, 13 Oct 2002 16:18:33 +0000 (UTC), simguy@cox.net (Bryan P
Walsh) wrote:
>Hi All,
>
>I was recently porting some code to a new linux compiler and was
>suprised when the following code failed to compile:
>
>template <class T>
>class A
>{
>public:
> A() : m_somedata(5) {}
> ~A() {}
>protected:
> T m_somedata;
>};
>
>template <class T>
>class B : public A<T>
>{
>public:
> B() : m_moredata(2) {}
> ~B() {}
> T getData() {return m_somedata + m_moredata;}
>protected:
> T m_moredata;
>};
>
>
>According to the vendor, the correct form for getData would be:
>
>T getData() {return A<T>::m_somedata + m_moredata;}
>
>The vendor references the following C++ standard issue.
>
>http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/cwg_defects.html#213
>
>The new form seems counter-intuitive me and I was suprised since the
>first form of getData works properly on all of the other compilers I've
>tried. Is the vendor interpreting the standard correctly? Is the first
>form really illegal?
Yes, according to the resolution of DR 213 the first form requires a
diagnostic at the point of definition. The resolution is part of TC1:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1367.html#213
Note that the pre-core213 wording was (emphasis mine):
"In the definition of a class template or in the definition of a
member of such a template **that appears outside of the template
definition**, if a base class of this template depends on a
template-parameter, the base class scope is not examined during name
lookup until the class template is instantiated"
So, if we limit ourselves to the point of definition the only reason
why the new wording affects your example is that you define getData()
*inside* the template definition. If you put it outside however, core
213 is irrelevant: you have an error at the point of definition
anyway.
With the new wording, the name m_somedata isn't found at the point of
instantiation either. To make it findable, you have to use a dependent
expression, e.g. by qualifying it with
A<T>::
or writing
this->m_somedata // 14.6.2.2
This is a case where the "this->" is not a matter of religion/coding
conventions/personal taste. You are not forced to use it, but it is
generally the less unreadable of the alternatives.
Having to write a lot of such "this->" in my everyday coding, I would
really like to see the following syntax proposed by Andrea Griffini
into C++ (well, I mean that .id-expression is considered exactly as
this->id-expression, even for the name lookup purposes we are
discussing in this thread)
http://groups.google.com/groups?threadm=3d6f113c.1181304%40news.tin.it
Genny.
---
[ 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 ]