 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
FT Guest
|
Posted: Mon Mar 20, 2006 2:27 am Post subject: Why the type of /this/ pointer not be X* const |
|
|
Hi all:
In the standuard 9.3.2, it says:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Mon Mar 20, 2006 3:29 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
FT wrote:
| Quote: |
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 ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Mon Mar 20, 2006 4:06 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
FT wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Mon Mar 20, 2006 4:49 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
"FT" <firetoucher.cn (AT) gmail (DOT) com> wrote in message
news:1142818774.781064.136080 (AT) j33g2000cwa (DOT) googlegroups.com...
| Quote: | 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 ] |
|
| Back to top |
|
 |
ThosRTanner Guest
|
Posted: Tue Mar 21, 2006 3:48 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
Andrew Koenig wrote:
| Quote: | "FT" <firetoucher.cn (AT) gmail (DOT) com> wrote in message
news:1142818774.781064.136080 (AT) j33g2000cwa (DOT) 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 ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Tue Mar 21, 2006 4:06 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
FT ha scritto:
| Quote: | 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".
| Quote: | 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 ] |
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Tue Mar 21, 2006 11:06 pm Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
"ThosRTanner" <ttanner2 (AT) bloomberg (DOT) net> wrote in message
news:1142942636.192887.102900 (AT) i39g2000cwa (DOT) googlegroups.com...
| Quote: | 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 ] |
|
| Back to top |
|
 |
Sergey P. Derevyago Guest
|
Posted: Thu Mar 23, 2006 3:15 am Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
Andrew Koenig wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Thu Mar 23, 2006 4:06 am Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
ThosRTanner wrote:
| Quote: |
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 ] |
|
| Back to top |
|
 |
Nicolas Pavlidis Guest
|
Posted: Sun Apr 16, 2006 5:06 am Post subject: Re: Why the type of /this/ pointer not be X* const |
|
|
Hi!
Disclaimer: I dont now if the things that I'll write below are a ral
argument, but I try it .
FT wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|