Topic: Type of this


Author: jpotter@falcon.lhup.edu (John Potter)
Date: Sat, 22 Feb 2003 21:05:23 +0000 (UTC)
Raw View
On Sat, 22 Feb 2003 20:23:11 +0000 (UTC), non-existent@iobox.com ("Sergey
P. Derevyago") wrote:

> According to 9.3.2p1: "The type of this in a member function of a class X is
> X*" but not X*const. However, I've tested my compilers with
> -----------------------------------8<-----------------------------------
> #include <stdio.h>

> typedef struct A* APtr;
> void f(APtr&)       { puts("f(APtr&)"); }
> void f(const APtr&) { puts("f(const APtr&)"); }

> struct A { void g() { f(this); } };

> int main()
> {
>  A a;
>  a.g();
> }
> -----------------------------------8<-----------------------------------
> They print "f(const APtr&)", i.e. the type of this is A*const.

No, the type is A* and it is not an lvalue.  A non-lvalue (what is
that if not an rvalue?) may not be bound to a non-const reference.
The non-const function is not a candidate.  Remove the const function
to get the diagnostic.

John

---
[ 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: philippe_mori@hotmail.com ("Philippe Mori")
Date: Sat, 22 Feb 2003 22:29:11 +0000 (UTC)
Raw View
"John Potter" <jpotter@falcon.lhup.edu> a    crit dans le message news:
3e57e299.6357703@news.earthlink.net...
> On Sat, 22 Feb 2003 20:23:11 +0000 (UTC), non-existent@iobox.com ("Sergey
> P. Derevyago") wrote:
>
> > According to 9.3.2p1: "The type of this in a member function of a class
X is
> > X*" but not X*const. However, I've tested my compilers with
> > -----------------------------------8<-----------------------------------
> > #include <stdio.h>
>
> > typedef struct A* APtr;
> > void f(APtr&)       { puts("f(APtr&)"); }
> > void f(const APtr&) { puts("f(const APtr&)"); }
>
> > struct A { void g() { f(this); } };
>
> > int main()
> > {
> >  A a;
> >  a.g();
> > }
> > -----------------------------------8<-----------------------------------
> > They print "f(const APtr&)", i.e. the type of this is A*const.
>
> No, the type is A* and it is not an lvalue.  A non-lvalue (what is
> that if not an rvalue?) may not be bound to a non-const reference.
> The non-const function is not a candidate.  Remove the const function
> to get the diagnostic.
>
> John
>
> ---

Effectively your test does not test what you want to test...

To test if this is X * or X * conxt, you should uses one of those 2 tests:

    a) typeid(this).name()

    b) or change g to something like:
        void g() { A another; this = &another; }


---
[ 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: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Sun, 23 Feb 2003 05:20:51 +0000 (UTC)
Raw View
"Sergey P. Derevyago" <non-existent@iobox.com> wrote...
> According to 9.3.2p1: "The type of this in a member function of a class X
is
> X*" but not X*const. However, I've tested my compilers with
> -----------------------------------8<-----------------------------------
> #include <stdio.h>
>
> typedef struct A* APtr;
> void f(APtr&)       { puts("f(APtr&)"); }
> void f(const APtr&) { puts("f(const APtr&)"); }
>
> struct A { void g() { f(this); } };
>
> int main()
> {
>  A a;
>  a.g();
> }
> -----------------------------------8<-----------------------------------
> They print "f(const APtr&)", i.e. the type of this is A*const.


'this' is not assignable, it is not a non-lvalue (9.3.2/1).  Since
it's _not_ an lvalue, it's _not_ convertible to <its_type*>&.

Victor
--
Please remove capital A's from my address when replying by mail

---
[ 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: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Mon, 24 Feb 2003 19:56:09 +0000 (UTC)
Raw View
John Potter wrote:
> > They print "f(const APtr&)", i.e. the type of this is A*const.
> No, the type is A* and it is not an lvalue.
 The non-const pointer behaves like a const pointer but it isn't... Are there
any good reasons for this madness? :)
 Well, I got the point: this defined to be an _expression_ not a pointer.  In
particular, we can't take address of this.

> A non-lvalue (what is
> that if not an rvalue?) may not be bound to a non-const reference.
> The non-const function is not a candidate.  Remove the const function
> to get the diagnostic.
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Sat, 22 Feb 2003 20:23:11 +0000 (UTC)
Raw View
According to 9.3.2p1: "The type of this in a member function of a class X is
X*" but not X*const. However, I've tested my compilers with
-----------------------------------8<-----------------------------------
#include <stdio.h>

typedef struct A* APtr;
void f(APtr&)       { puts("f(APtr&)"); }
void f(const APtr&) { puts("f(const APtr&)"); }

struct A { void g() { f(this); } };

int main()
{
 A a;
 a.g();
}
-----------------------------------8<-----------------------------------
They print "f(const APtr&)", i.e. the type of this is A*const.
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.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://www.jamesd.demon.co.uk/csc/faq.html                       ]