Re: type of the 'this' pointer
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! ]