473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const_cast versus compiler optimization

Hello,

I wonder if and in what cases casting const away by a const_cast can be
dangerous. In one place the only way I can imagine to realize a pretty
nice feature is casting away const. Now I wonder if this is dangerous. I
have something like that:

const Object &
Object::dummy(const AType &a) const
{
Object &refThis = const_cast<Object &>(*this);
AType &refA = const_cast<AType &>(rhs);
refThis.member = a.member();
...
}

Are there situations, where these constructs are dangerous?
I know, this code is ugly. But the realized feature is worth it.

regards,
Alex
Jul 23 '05 #1
6 1910
Alexander Stippler wrote:
I wonder if and in what cases casting const away by a const_cast can be
dangerous. In one place the only way I can imagine to realize a pretty
nice feature is casting away const. Now I wonder if this is dangerous.
It is no more (and no less) dangerous than any other undefined behaviour.
I
have something like that:

const Object &
Object::dummy(const AType &a) const
{
Object &refThis = const_cast<Object &>(*this);
AType &refA = const_cast<AType &>(rhs);
refThis.member = a.member();
...
}

Are there situations, where these constructs are dangerous?
I know, this code is ugly. But the realized feature is worth it.


It's not just ugly. It's simply wrong. You should at least be consistent
with your names. Did you mean

const Object &
Object::dummy(const AType &a) const
{
Object &refThis = const_cast<Object &>(*this);
AType &refA = const_cast<AType &>(a);
refThis.member = refA.member();
...
}

?

To answer your question, it's dangerous where 'a' or 'this' were _created_
as 'const'. The compiler is allowed to place any parts of the object in
read-only part of program memory when the object is declared 'const'.
Assigning to 'refThis.member' causes undefined behaviour.

V
Jul 23 '05 #2

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:20********************@news.uni-ulm.de...
Hello,

I wonder if and in what cases casting const away by a const_cast can be
dangerous. In one place the only way I can imagine to realize a pretty
nice feature is casting away const. Now I wonder if this is dangerous. I
have something like that:

const Object &
Object::dummy(const AType &a) const
{
Object &refThis = const_cast<Object &>(*this);
AType &refA = const_cast<AType &>(rhs);
refThis.member = a.member();
...
}

Are there situations, where these constructs are dangerous?
I know, this code is ugly. But the realized feature is worth it.


Why would you make the function const if it's going to modify the object
it's called on? And why use the local references at all? It looks like all
you're doing is this:

const Object & Object::dummy(const AType &a)
{
member = a.member();
...
}

-Howard
Jul 23 '05 #3

"Howard" <al*****@hotmail.com> wrote in message
news:Dp*********************@bgtnsc04-news.ops.worldnet.att.net...

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:20********************@news.uni-ulm.de...
Hello,

I wonder if and in what cases casting const away by a const_cast can be
dangerous. In one place the only way I can imagine to realize a pretty
nice feature is casting away const. Now I wonder if this is dangerous. I
have something like that:

const Object &
Object::dummy(const AType &a) const
{
Object &refThis = const_cast<Object &>(*this);
AType &refA = const_cast<AType &>(rhs);
refThis.member = a.member();
...
}

Are there situations, where these constructs are dangerous?
I know, this code is ugly. But the realized feature is worth it.


Why would you make the function const if it's going to modify the object
it's called on? And why use the local references at all? It looks like
all you're doing is this:

const Object & Object::dummy(const AType &a)
{
member = a.member();
...
}


I should have said, "it looks like all you're TRYING to do is this:"

-Howard

Jul 23 '05 #4
Howard wrote:

Why would you make the function const if it's going to modify the object
it's called on? And why use the local references at all? It looks like all
you're doing is this:

const Object & Object::dummy(const AType &a)
{
member = a.member();
...
}


Possibly he's creating a functor. Of course, the correct way to do that
is to make the particular item to be updated mutable.
Jul 23 '05 #5
...
Why would you make the function const if it's going to modify the
object it's called on? And why use the local references at all? It
looks like all you're doing is this:

const Object & Object::dummy(const AType &a)
{
member = a.member();
...
}


I should have said, "it looks like all you're TRYING to do is this:"

-Howard


Not really. It's hard to desribe in a few lines. So I use an example:
I want objects, which refer to other objects of the same type, like the
following by a member function:

Object &
referTo(Object &rhs);

Object oneO, otherO;
oneO.referTo(otherO);

At that point, oneO is a "pointer into" otherO. No problem, as long as
otherO is not const. But if otherO were const, I would like to have oneO
refer to otherO, but oneO should not be able to change otherO. So I make
oneO const, but then I need the const_cast ...
An abuse of const, one might say and he would probably be right. What I
want to implement is a reference, where the referenced object is const (
not modifiable through the reference), not the reference object. Why not
a type of its own for the references? Because all classes are template
classes and there would be plenty of complications, because the
reference shall be used just like the original object and type
conversions for templates together with instantiation ...
Tried to explain my motivation, but must sounds strange and like bad
design, or?
But I have now found a standard conform solution :-).

regards,
Alex
Jul 23 '05 #6
Alexander Stippler wrote:
Are there situations, where these constructs are dangerous?


Yes: if you have any doubt, is dangerous. You must use a cast only when you
are sure that is safe. The casts tell the compiler: "I really know what I'm
doing".

The key is: don't cast away constness to modify an object that is const.

--
Salu2
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
7
by: R. Anbeeswaran | last post by:
Hi All, void main() { const int i = 20; int *p = const_cast<int*>(&i); *p = 40; cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n"; }
6
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
5
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
2
by: puru | last post by:
I have a template class like below. template <TInt Sclass XYZ { } const XYZ<10abc; //Instance of the class How do remove the constantness of abc using const_cast. I mean, I want the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.