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: const reference typedef compile error: - comp.lang.c++.moderated ...To understand the situation even better, try pointers instead of references: typedef int value_type; typedef value_type* ptr; typedef const value ... procedure() pointer - comp.lang.fortranHi, i am trying to do the following, with no success whatsoever: create a type (myType), that has a procedure() pointer. Then this pointer must po... size of a derived type containing pointers... - comp.lang.fortran ...Hello, In my code I need to determine the size of various variables which are defined as derived types. Until now I used to use the INQUIRE() f... Down-cast a polymorphic pointer? - comp.lang.fortran(Well, the Standard does not specify implementation, of course, but how else would compiler know of which child type is the pointer) So, in my case, since my objects ... class definition containing member of own type - comp.lang.c++ ...size of a derived type containing pointers... - comp.lang.fortran ..... work if the derived type contains some ... but that's not the main definition of ... const B' to ... Why can not I implicitly convert "Derived **" to "Base * const ...size of a derived type containing pointers... - comp.lang.fortran ... Why can not I implicitly convert "Derived **" to "Base * const ... size of a derived type containing ... CONTAINING_RECORD macro - comp.lang.c++.moderatedIn C++ NULL must not be > macro defined as (void *)0 because in C++ unlike C, there are > no implicit conversions from void* to other types of pointer. Pointer to function Typedefs - comp.lang.c++.moderatedI have seen lot of definition like the one below in code typedef void (*func_cback) (arg1, arg2) I need to know what this means in layman terms Th... Neatest way to get the end pointer? - comp.lang.cAssigning a compile-time 0 to a pointer type is guaranteed to result in a NULL pointer constant. -- "Okay, buzzwords only. Two syllables, tops." What is "plain old data" type (POD)? - comp.lang.c++.moderated ...Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively ... Type of this Pointer (C++) - Microsoft Corporation: Software ...The this pointer's type can be modified in the function declaration by the const and volatile keywords. To declare a function as having the attributes of one or more ... Types of Mouse Pointers | eHow.comSimilar to computer technology, mouse pointers have evolved in time, to help users understand what operation the mouse will have. From the initial arrow, cursors have ... 7/28/2012 2:52:35 PM
|