Topic: Why the type of /this/ pointer not be X* const


Author: aon.912719634@aon.at (Nicolas Pavlidis)
Date: Sun, 16 Apr 2006 04:23:13 GMT
Raw View
Hi!

Disclaimer: I dont now if the things that I'll write below are a ral
argument, but I try it :-).

FT wrote:
> Hi all:
> In the standuard 9.3.2, it says:
>> 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*.
>
> IMO, the /this/ pointer can not be used as a l-value, so its type
> should be X* const
> instead of X*.(accordingly, const X* const  instead of const X*,etc...)
>
> Why the standuard "omit" that "const"?


Think of general scoping in C++, AFAIK if you have cod such as:

namespace Test_
{
  int foo() {}

  class MyClass
  {
  public:
    int bar()
    {
      foo();
    }
  private:
    int foo() {}
  };
}

it is possibe that the Test_::foo() function will be called, but you
expected, that test_::MyClass::foo() is called in bar();

Making this const would cause that private methods coud not be called if
they change data inside the object of e.g. MyClass. So making this
const, would lead to alot of problems compiling exsisting (and corect)
OO code IMHO, but maybe I'm wrong.

Best regards,
Nicolas

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "ThosRTanner" <ttanner2@bloomberg.net>
Date: Tue, 21 Mar 2006 09:48:30 CST
Raw View
Andrew Koenig wrote:
> "FT" <firetoucher.cn@gmail.com> wrote in message
> news:1142818774.781064.136080@j33g2000cwa.googlegroups.com...
>
> > IMO, the /this/ pointer can not be used as a l-value, so its type
> > should be X* const
> > instead of X*.(accordingly, const X* const  instead of const X*,etc...)
>
> > Why the standuard "omit" that "const"?
>
> Suppose the standard were rewritten the way you would like.  Can you show us
> an example of a program that would behave differently?  If you can, please
> do so.  If not, the rewrite makes no difference, so there is no need for it.

Surely it's not a question of whether it would make programs behave
differently, it's a question of internal consistency. Other objects
that are not const can be modified. But this, although it is not
apparently const, cannot be modified. If compilers didn't treat it as
const, you could do
     this = this + 1;
which you can't. So it looks as though this is a const. But the
standard appears to say that it isn't...

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: ark@acm.org ("Andrew Koenig")
Date: Tue, 21 Mar 2006 22:30:42 GMT
Raw View
"ThosRTanner" <ttanner2@bloomberg.net> wrote in message
news:1142942636.192887.102900@i39g2000cwa.googlegroups.com...

>> Suppose the standard were rewritten the way you would like.  Can you show
>> us
>> an example of a program that would behave differently?  If you can,
>> please
>> do so.  If not, the rewrite makes no difference, so there is no need for
>> it.

> Surely it's not a question of whether it would make programs behave
> differently, it's a question of internal consistency. Other objects
> that are not const can be modified. But this, although it is not
> apparently const, cannot be modified. If compilers didn't treat it as
> const, you could do
>     this = this + 1;
> which you can't. So it looks as though this is a const.

Why do you think so?  Using this as the target of an assignment is
ill-formed because this is not an lvalue, irrespective of whether it is
const.  If I write

    int* foo();

and then I try to execute

    foo() = 0;

the compiler should complain because foo() is not an lvalue, even though it
is not const.  The behavior here is consistent with the behavior of this.

So I would like to insist on an answer to my question:  Can you show me an
example of a program that would behave differently if this were const?

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: Wed, 22 Mar 2006 21:15:12 CST
Raw View
Andrew Koenig wrote:
> So I would like to insist on an answer to my question:  Can you show me an
> example of a program that would behave differently if this were const?
>
 Probably (currently unavailable) typeof() would show the difference.
--
         With all respect, Sergey.               http://ders.angen.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.comeaucomputing.com/csc/faq.html                      ]





Author: musiphil@bawi.org (Seungbeom Kim)
Date: Thu, 23 Mar 2006 03:17:31 GMT
Raw View
ThosRTanner wrote:
>
> Surely it's not a question of whether it would make programs behave
> differently, it's a question of internal consistency. Other objects
> that are not const can be modified. But this, although it is not
> apparently const, cannot be modified. If compilers didn't treat it as
> const, you could do
>      this = this + 1;
> which you can't. So it looks as though this is a const. But the
> standard appears to say that it isn't...

Neither can you modify non-lvalues nor take their address,
even though their types are not explicitly const, just as

    36 = 36
    36++
    &36

are invalid.

--
Seungbeom Kim

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "FT" <firetoucher.cn@gmail.com>
Date: Sun, 19 Mar 2006 20:27:34 CST
Raw View
Hi all:
In the standuard 9.3.2, it says:
>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*.

IMO, the /this/ pointer can not be used as a l-value, so its type
should be X* const
instead of X*.(accordingly, const X* const  instead of const X*,etc...)

Why the standuard "omit" that "const"?

(It's my first time to post on USENET. Hope me have not done any stupid
thing.)

Thanks,

FT

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: musiphil@bawi.org (Seungbeom Kim)
Date: Mon, 20 Mar 2006 15:22:04 GMT
Raw View
FT wrote:
> Hi all:
> In the standuard 9.3.2, it says:
>> 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*.
>
> IMO, the /this/ pointer can not be used as a l-value, so its type
> should be X* const
> instead of X*.(accordingly, const X* const  instead of const X*,etc...)
>
> Why the standuard "omit" that "const"?

The first-level const applies only to lvalues. For example, we don't say
that the type of 36 is const int, but it's simply an int.

--
Seungbeom Kim

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: Pete Becker <petebecker@acm.org>
Date: Mon, 20 Mar 2006 09:29:31 CST
Raw View
FT wrote:
>
> Why the standuard "omit" that "const"?
>

In the olden days it wasn't const. When you wanted to provide memory for
an object in a non-standard way you could assign to this. That was
replaced by overloading operator new, so the need went away.

--

Pete Becker
Roundhouse Consulting, Ltd.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Andrew Koenig" <ark@acm.org>
Date: Mon, 20 Mar 2006 10:49:05 CST
Raw View
"FT" <firetoucher.cn@gmail.com> wrote in message
news:1142818774.781064.136080@j33g2000cwa.googlegroups.com...

> IMO, the /this/ pointer can not be used as a l-value, so its type
> should be X* const
> instead of X*.(accordingly, const X* const  instead of const X*,etc...)

> Why the standuard "omit" that "const"?

Suppose the standard were rewritten the way you would like.  Can you show us
an example of a program that would behave differently?  If you can, please
do so.  If not, the rewrite makes no difference, so there is no need for it.


---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 21 Mar 2006 15:44:27 GMT
Raw View
FT ha scritto:
> Hi all:
> In the standuard 9.3.2, it says:
>> 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*.
>
> IMO, the /this/ pointer can not be used as a l-value, so its type
> should be X* const
> instead of X*.(accordingly, const X* const  instead of const X*,etc...)

It's not your opinion that /this/ can't be used as an lvalue, it's
written few words before the sentence you quoted, right in 9.3.2/1: "the
keyword this is a non-lvalue expression".

> Why the standuard "omit" that "const"?

Because the const is unnecessary. As /this/ is a non-lvalue, it can't be
modified anyway and top-level qualifiers make sense only on lvalues.

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]