Board index » cppbuilder » Re: Using const operator[] inside non-const operator[]

Re: Using const operator[] inside non-const operator[]


2008-02-28 12:35:49 AM
cppbuilder99
Dennis Cote < XXXX@XXXXX.COM >wrote:
Quote
The second would be that it does accept a const_cast in that statement,
and uses it to add constness to the object.
No, it would be a bug according to the standard if it *didn't* allow the
const cast.
(And all other compilers I've tested, including Comeau, permit this.)
5.2.11 [expr.const.cast] has two relevant paras.
Para 3 basically says you can *change* the cv (const and volatile)
qualification of a pointer.
Para 4 basically says that if you can change from one reference type to
a second reference type if the corresponding pointer conversion is
legal.
The reason for the existence of the const_cast is to be able to change
the cv-qualification of a type. It *is* mostly used for casting away
const, and that's therefore the use that's usually mentioned.
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK
 
 

Re:Re: Using const operator[] inside non-const operator[]

Alan Bellingham wrote:
Quote

No, it would be a bug according to the standard if it *didn't* allow the
const cast.

I stand corrected. If the standard does allow a const_cast to add
constness to an object, the second point is not a bug.
I did say I wasn't aware of what standard says, so thank you for
clarifying that.
Dennis Cote
 

Re:Re: Using const operator[] inside non-const operator[]

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
Quote
So it is a bug of BCB is not it?
Yes, I've filed it as QC #58821:
qc.codegear.com/wc/qcmain.aspx
Jason
 

{smallsort}

Re:Re: Using const operator[] inside non-const operator[]

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
news:47c54b32$ XXXX@XXXXX.COM ...
>So it is a bug of BCB is not it?
Just to add to this (I mentioned it in the report), it's not limited to
user-defined types. The following is also incorrectly rejected:
void H (void) {
int i;
int &j = const_cast<int &>(static_cast<const int &>(i));
};
It's any time you use static_cast/dynamic_cast to add cv to a reference
(problem exists with volatile too), then const_cast to remove it again, all
in the same statement.
Jason
 

Re:Re: Using const operator[] inside non-const operator[]

"Jason Cipriani" < XXXX@XXXXX.COM >wrote:
Quote
void H (void) {
int i;
int &j = const_cast<int &>(static_cast<const int &>(i));
};
Yep, I saw that too.
Quote
It's any time you use static_cast/dynamic_cast to add cv to a reference
(problem exists with volatile too), then const_cast to remove it again, all
in the same statement.
Ah, neat. Very neat.
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK