type of the 'this' pointer

  • Follow


Hello all
I am a little confuse as for the exact type of the this pointer.
according to stroustrup's book : "in a non-const member function of
class X, the type of this is X* " (page 231)
that is also what the standard doc says: "The type of this in a member
function of
a class X is X*."  (9.3.2)
but in many other references the type of this is said to be  X* const
(msdn for example or IBM Linux compiler docs :
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/co
m.ibm.xlcpp8l.doc/language/ref/cplr035.htm

so is it type X* or X* const ???

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Eldar 7/26/2009 12:48:14 PM

On 26 Lip, 21:48, Eldar Ronen <eldar.ro...@gmail.com> wrote:
> Hello all
> I am a little confuse as for the exact type of the this pointer.
> according to stroustrup's book : "in a non-const member function of
> class X, the type of this is X* " (page 231)
> that is also what the standard doc says: "The type of this in a member
> function of
> a class X is X*."  (9.3.2)
> but in many other references the type of this is said to be  X* const
> (msdn for example or IBM Linux compiler docs
:http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?to...
> m.ibm.xlcpp8l.doc/language/ref/cplr035.htm
>
> so is it type X* or X* const ???
>

It probably varies between compilers. I haven't met a compiler that
treats this pointer in a method of class X as const X*.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Marek 7/26/2009 1:06:27 PM


On 26 Jul., 21:48, Eldar Ronen <eldar.ro...@gmail.com> wrote:
> Hello all
> I am a little confuse as for the exact type of the this pointer.
> according to stroustrup's book : "in a non-const member function of
> class X, the type of this is X* " (page 231)
> that is also what the standard doc says: "The type of this in a member
> function of
> a class X is X*."  (9.3.2)
> but in many other references the type of this is said to be  X* const
> (msdn for example or IBM Linux compiler docs :http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?to...
> m.ibm.xlcpp8l.doc/language/ref/cplr035.htm
>
> so is it type X* or X* const ???
>
It's "X*". It cannot be "C *const", because it's an rvalue expression
of pointer type, and non-class-type rvalue expressions never have cv-
qualified types. Imagine "this" as being something like the following,
conceptually:

     T *self; // internal magic
     #define this (self + 0)

Now, the addition yields an rvalue. This is what forbids doing "this
= ...;". Some people say even that "this" is not a pointer, but merely
an address (the value of a pointer). Well, this probably is hair
splitting. The Standard itself refers to "this" as being a pointer.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply litb 7/26/2009 7:38:35 PM

On Jul 26, 3:48 pm, Eldar Ronen <eldar.ro...@gmail.com> wrote:
> the standard doc says: "The type of this in a member
> function of a class X is X*."  (9.3.2)
More than that, it says that "this" is a non-lvalue expression.
You're in the right part of the standard, just probably missed
that bit. Or maybe I have a newer draft.

So the const-ness is irrelevant -- it's an rvalue.

> so is it type X* or X* const ???

The former, unless the member function is const where it will be
const X *.

--Jonathan


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Jonathan 7/26/2009 7:38:57 PM

On Jul 26, 12:48 pm, Eldar Ronen <eldar.ro...@gmail.com> wrote:
> Hello all
> I am a little confuse as for the exact type of the this pointer.
> according to stroustrup's book : "in a non-const member function of
> class X, the type of this is X* " (page 231)
> that is also what the standard doc says: "The type of this in a member
> function of
> a class X is X*."  (9.3.2)
> but in many other references the type of this is said to be  X* const
> (msdn for example or IBM Linux compiler docs :http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?to...
> m.ibm.xlcpp8l.doc/language/ref/cplr035.htm
>
> so is it type X* or X* const ???

The type of "this" pointer depends on const-volatile qualification of
the member function itself:

"The type of 'this' in a member function of a class X is 'X*'. If the
member function is declared const, the type of 'this' is 'const X*',
if the member function is declared volatile, the type of 'this' is
'volatile X*', and if the member function is declared const volatile,
the type of 'this' is 'const volatile X*'."[�9.3.2/1]

Greg


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Greg 7/26/2009 7:39:37 PM

On 27 Jul., 04:38, litb wrote:
>
> It's "X*". It cannot be "C *const", because it's an rvalue expression
> of pointer type, and non-class-type rvalue expressions never have cv-
> qualified types.

G++ (version 3.4.5 and 4.3.3) seems to handle "this" as a const rvalue
and supports const rvalues w.r.t. template argument deduction:

   #include <iostream>

   class clazz;

   template<typename T>
   struct foo {
     static void bar() {std::cout << "unknown\n";}
   };

   template<> struct foo<clazz*> {
     static void bar() {std::cout << "clazz *\n";}
   };

   template<> struct foo<clazz const*> {
     static void bar() {std::cout << "clazz const *\n";}
   };

   template<> struct foo<clazz*const> {
     static void bar() {std::cout << "clazz * const\n";}
   };

   template<> struct foo<clazz const*const> {
     static void bar() {std::cout << "clazz const * const\n";}
   };

   template<typename T> void xxx(T &) {
     foo<T>::bar();
   }

   clazz* const nop(clazz* p) {return p;}

   class clazz {
   public:
     void mf1()       {xxx(this);}
     void mf2() const {xxx(this);}
     void mf3()       {xxx(nop(this+0));}

     // error: invalid initialization of non-const reference
     // void mf4() {xxx(this+0);}

     // error: non-lvalue in unary `&'
     // void mf5() {&this;}
   };

   int main() {
     clazz c;
     c.mf1();
     c.mf2();
     c.mf3();
   }

The output is

   clazz * const
   clazz const * const
   clazz * const


Cheers!
SG

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply SG 7/27/2009 4:13:29 PM

5 Replies
124 Views

(page loaded in 0.063 seconds)

Similiar Articles:













7/28/2012 2:52:35 PM


Reply: