Topic: this-> required?
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/05/05 Raw View
Srinivas Vobilisetti wrote:
>
> asbinn@rstcorp.com wrote:
>
> > In article <354657D6.C45E924F@xilinx.com>,
> > Lou Sanchez-chopitea <lou.sanchez-chopitea@xilinx.com> wrote:
> >
> > > template <class T> class Base
> > > {
> > > public:
> > > int fBase ( void) { return 1;}
> > > };
> > >
> > > template <class T> class Derived : public Base<T>
> > > {
> > > public:
> > > int f1 ( void) { return fBase();}
> > > int f2 ( void) { return this->fBase();}
> > > };
> > >
> >
> > As far as correctness goes, it looks syntactically and semantically
> > correct to me.
> >
> > A consideration is related to the recent Guru of the Week. When class
> > Derived<T> is parsed, the parent class Base<T> is not considering during
> > name look-up [temp.dep 14.6.2 paragrph 3]. This means that if there was a
> > function
> >
> > int fBase();
> >
> > declared before the definition of Derived<T>, then it would be bound to
> > the use of fBase() in the definition of Derived<T>::f1(). Pretty unintuitive
> > to me.
> >
> > Any comments from committee members on the reasoning behind this?
> >
>
> I am not a committee member. Probable reasoning behind this is that
> aspecialization of class Base<T> may not have the member you are referring to
> in the class Derived<T> .
But as soon as you are instantiating the template, you know if there
is one. So why not simply defer the decision to this place? The
behaviour now is really anti-intuitive (not just unintuitive).
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: 1998/04/29 Raw View
Lou Sanchez-chopitea wrote:
>
>
> In particular, is the this-> in f1 required?
No.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/04/29 Raw View
Lou Sanchez-chopitea wrote:
> Is the following snippet of code correct?
no, f1 isn't valid (but will work with a lot of
todays compilers)
> template <class T> class Base
> {
> public:
> int fBase ( void) { return 1;}
> };
>
> template <class T> class Derived : public Base<T>
> {
> public:
> int f1 ( void) { return fBase();}
> int f2 ( void) { return this->fBase();}
> };
>
> In particular, is the this-> in f1 required? Thanks.
Yes, because othewise fBase() is a non-dependant name
bound in the environement of the definition, not of the
instanciation.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: asbinn@rstcorp.com
Date: 1998/04/29 Raw View
In article <354657D6.C45E924F@xilinx.com>,
Lou Sanchez-chopitea <lou.sanchez-chopitea@xilinx.com> wrote:
> template <class T> class Base
> {
> public:
> int fBase ( void) { return 1;}
> };
>
> template <class T> class Derived : public Base<T>
> {
> public:
> int f1 ( void) { return fBase();}
> int f2 ( void) { return this->fBase();}
> };
>
As far as correctness goes, it looks syntactically and semantically
correct to me.
A consideration is related to the recent Guru of the Week. When class
Derived<T> is parsed, the parent class Base<T> is not considering during
name look-up [temp.dep 14.6.2 paragrph 3]. This means that if there was a
function
int fBase();
declared before the definition of Derived<T>, then it would be bound to
the use of fBase() in the definition of Derived<T>::f1(). Pretty unintuitive
to me.
Any comments from committee members on the reasoning behind this?
> In particular, is the this-> in f1 required? Thanks.
I don't think it's required, but could be a way to make sure a fBase() from
the enclosing scope isn't called.
Maybe using Base<T>::fBase() would ensure that intended fBase() is called.
Aaron
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/04/29 Raw View
Lou Sanchez-chopitea <lou.sanchez-chopitea@xilinx.com> writes:
> Is the following snippet of code correct?
yes and no.
> template <class T> class Derived : public Base<T>
> int f1 ( void) { return fBase();}
> In particular, is the this-> in f1 required? Thanks.
Nope, it is not required, but it is advisable. Because Base<T> is a
type-dependent base class, its scope is only examined at
template-instantiation time. If there were a function or type named
::fBase, the usage of fBase in f1 would use this definition instead of
the one in the base class. See [temp.dep]/3 for more details.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: dHarrison@worldnet.att.net (Doug Harrison)
Date: 1998/04/29 Raw View
On 28 Apr 1998 22:51:36 GMT, Lou Sanchez-chopitea
<lou.sanchez-chopitea@xilinx.com> wrote:
>Hi,
> Is the following snippet of code correct?
>
>template <class T> class Base
>{
>public:
> int fBase ( void) { return 1;}
>};
>
>template <class T> class Derived : public Base<T>
>{
>public:
> int f1 ( void) { return fBase();}
> int f2 ( void) { return this->fBase();}
>};
>
> In particular, is the this-> in f1 required? Thanks.
Yes, it is. The given definition of f1() is illegal, because fBase is
non-dependent and is thus bound right then and there, but dependent
base classes aren't searched at that time, and there's nothing to bind
it to. Moreover, if there was an fBase at namespace scope, preceding
the definition of Derived::f1(), that's the one you would get in f1().
See the FDIS, [temp.dep], 14.6.2/3, and [temp.res], 14.6/9. Besides
this->, in this case, you could also use Base<T>::fBase().
--
Doug Harrison
dHarrison@worldnet.att.net
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bill@gibbons.org (Bill Gibbons)
Date: 1998/04/29 Raw View
In article <354657D6.C45E924F@xilinx.com>, Lou Sanchez-chopitea
<lou.sanchez-chopitea@xilinx.com> wrote:
> Is the following snippet of code correct?
>
> template <class T> class Base
> {
> public:
> int fBase ( void) { return 1;}
> };
>
> template <class T> class Derived : public Base<T>
> {
> public:
> int f1 ( void) { return fBase();}
> int f2 ( void) { return this->fBase();}
> };
>
> In particular, is the this-> in f1 required? Thanks.
In article <3546960D.9B44837C@acm.org>, Pete Becker <petebecker@acm.org> wrote:
> Lou Sanchez-chopitea wrote:
> >
> >
> > In particular, is the this-> in f1 required?
>
> No.
Yes, it is.
The contents of "Base<T>" are NOT visible in the template definition
itself, because there is no guarantee that "Base<T>" will be an
instantiation of the template "Base". It could be an instantiation
of an explicit or partial specialization. The actual template from
which "Base<T>" is instantiated depeends on "T", which isn't known
until Derived<T> is instantiated. In fact, "Base<T>" might not even
have a member "fBase" for a particular "T".
Some existing commercial compilers get this wrong. They are attempting
to compile older code written before the template rules were completely
written (when templates were more like macros). But such compilers can
actually generate incorrect code, and they encourage people to continue
writing ill-formed code such as that above. So in my opinion vendors
which supply compilers that accept such cod are not doing their customers
a favor.
-- Bill Gibbons
bill@gibbons.org
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Srinivas Vobilisetti <srinivas-v@usa.net>
Date: 1998/05/02 Raw View
asbinn@rstcorp.com wrote:
> In article <354657D6.C45E924F@xilinx.com>,
> Lou Sanchez-chopitea <lou.sanchez-chopitea@xilinx.com> wrote:
>
> > template <class T> class Base
> > {
> > public:
> > int fBase ( void) { return 1;}
> > };
> >
> > template <class T> class Derived : public Base<T>
> > {
> > public:
> > int f1 ( void) { return fBase();}
> > int f2 ( void) { return this->fBase();}
> > };
> >
>
> As far as correctness goes, it looks syntactically and semantically
> correct to me.
>
> A consideration is related to the recent Guru of the Week. When class
> Derived<T> is parsed, the parent class Base<T> is not considering during
> name look-up [temp.dep 14.6.2 paragrph 3]. This means that if there was a
> function
>
> int fBase();
>
> declared before the definition of Derived<T>, then it would be bound to
> the use of fBase() in the definition of Derived<T>::f1(). Pretty unintuitive
> to me.
>
> Any comments from committee members on the reasoning behind this?
>
I am not a committee member. Probable reasoning behind this is that
aspecialization of class Base<T> may not have the member you are referring to
in the class Derived<T> .
Srinivas
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Lou Sanchez-chopitea <lou.sanchez-chopitea@xilinx.com>
Date: 1998/04/28 Raw View
Hi,
Is the following snippet of code correct?
template <class T> class Base
{
public:
int fBase ( void) { return 1;}
};
template <class T> class Derived : public Base<T>
{
public:
int f1 ( void) { return fBase();}
int f2 ( void) { return this->fBase();}
};
In particular, is the this-> in f1 required? Thanks.
Cheers
Lou
--
Lou Sanchez-Chopitea EMail: lou.sanchez-chopitea@xilinx.com
Senior Software Engineer SnailMail: 2100 Logic Drive
SpeakMail: (408) 879-5059 San Jose, CA 95124
FaxMail: (408) 377-4790
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]