473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

does this have undefined behaviour

Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."

double& d = const_cast<doub le&>(ref);

Kindly clarify.

Thanks
V.Subramanian
Dec 21 '07 #1
10 1791
su************* *@yahoo.com wrote:
Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."

double& d = const_cast<doub le&>(ref);
What you're doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can't find any place in the Standard that would prohibit
such functionality. Then my guess is that it's allowed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '07 #2
On Dec 21, 5:04 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
subramanian10.. .@yahoo.com wrote:
Consider the following code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;
return EXIT_SUCCESS;
}
In both g++ and VC++ 2005 Express Edition, the output is
100
1.1
But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."
double& d = const_cast<doub le&>(ref);

What you're doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can't find any place in the Standard that would prohibit
such functionality. Then my guess is that it's allowed.
I think the OP might have thought that he actually changed the value
of "ref" (the constant one). I don't think he realized that a
temporary was created - therefore changing the value of "d" does
not change the value of "ref".

Regards,

Werner

Dec 21 '07 #3
werasm wrote:
On Dec 21, 5:04 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>subramanian10. ..@yahoo.com wrote:
>>Consider the following code:
>>#include <iostream>
#include <cstdlib>
>>using namespace std;
>>int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;
>>return EXIT_SUCCESS;
}
>>In both g++ and VC++ 2005 Express Edition, the output is
100
1.1
>>But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."
>>double& d = const_cast<doub le&>(ref);

What you're doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can't find any place in the Standard that would prohibit
such functionality. Then my guess is that it's allowed.

I think the OP might have thought that he actually changed the value
of "ref" (the constant one). I don't think he realized that a
temporary was created - therefore changing the value of "d" does
not change the value of "ref".
No, that's incorrect, IMO. First off, you can't change the value
of ref because it's a reference, it doesn't really have a value
(aside from the fact that it refers to some particular object).
The temporary created is of type 'double', it has initial value
of 100.00 and it starts its lifetime just before 'ref' is bound
to it. The integral expression '100' is the initialiser for that
temporary.

The temporary to which 'ref' is bound is not a const object. It
is perfectly OK to change its value. Whether it makes sense to
do so or not is another question, but the idea is the same as in

void foo(int const& rci)
{
int& ri = const_cast<int& >(rci);
ri = 666; /// Is this OK? -- I say, yes, it is!
}

#include <iostream>
#include <ostream>
int main() {
int a = 42;
std::cout << "Before a = " << a << std::endl;
foo(a);
std::cout << "After a = " << a << std::endl;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '07 #4
"su************ **@yahoo.com, India" <su************ **@yahoo.comwro te
in news:404f0505-3d9f-4173-8361-a0a328537882
@i29g2000prf.go oglegroups.com:
Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);
Dangerous. You might attempt to modify what d refers to.
cout << d << endl;
d = 1.1;
Undefined behaviour. You are modifying a const object.
cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."

double& d = const_cast<doub le&>(ref);
This isn't Undefined Behaviour. Attempting to modify what d refers to
is the Undefined Behaviour.
Dec 24 '07 #5
On Dec 21, 5:04 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
subramanian10.. .@yahoo.com wrote:
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);

What you're doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
The temporary is const. See 8.3.5#5 [dcl.init.ref], last clause.

So the behaviour is undefined when the attempt is made to
modify d (the cast itself is fine though).
Dec 25 '07 #6
On Dec 22, 3:10 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
The temporary to which 'ref' is bound is not a const object. It
is perfectly OK to change its value.
Actually it is const, as per 8.3.5#5
the idea is the same as in

void foo(int const& rci)
{
int& ri = const_cast<int& >(rci);
ri = 666; /// Is this OK? -- I say, yes, it is!
}

#include <iostream>
#include <ostream>
int main() {
int a = 42;
std::cout << "Before a = " << a << std::endl;
foo(a);
std::cout << "After a = " << a << std::endl;
}
This code is OK; since a is an lvalue, a temporary
is not created.
Dec 25 '07 #7
Andre Kostur wrote:
"su************ **@yahoo.com, India" <su************ **@yahoo.comwro te
in news:404f0505-3d9f-4173-8361-a0a328537882
@i29g2000prf.go oglegroups.com:
>Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<doub le&>(ref);

Dangerous. You might attempt to modify what d refers to.
> cout << d << endl;
d = 1.1;

Undefined behaviour. You are modifying a const object.
> cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using 'd' invoke undefined behavior or is it
valid."

double& d = const_cast<doub le&>(ref);

This isn't Undefined Behaviour. Attempting to modify what d refers to
is the Undefined Behaviour.
Could you please elaborate on why it is undefined behaviour?

Thanks.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 26 '07 #8
>
The temporary to which 'ref' is bound is not a const object. It
is perfectly OK to change its value. Whether it makes sense to
do so or not is another question, but the idea is the same as in
I think, as the temp object is that of an in-built type, it is fine.
But had it been that of a custom type, the temp object would have been
an constant object...
Dec 26 '07 #9
Rahul wrote:
>The temporary to which 'ref' is bound is not a const object. It
is perfectly OK to change its value. Whether it makes sense to
do so or not is another question, but the idea is the same as in

I think, as the temp object is that of an in-built type, it is fine.
But had it been that of a custom type, the temp object would have been
an constant object...
Any standard quote to support that claim?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 26 '07 #10

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

Similar topics

8
5977
by: puzzlecracker | last post by:
interesting case: class MyString{ char * strRep; // initialized to char array public: ~MyString(){delete strRep;} //why would this work // just like 'delete strRep;' };
11
7605
by: becte | last post by:
What does the standard say about f(x++) ? 1. x is incremented and then the that value is used in function f, i.e. x++; f(x); 2. the value of x (before ++) is send to f and after returning. x is increased, i.e f(x); x++; 3. undefined behavior, it is different for different compilers I encountered the problem when I was porting code (not...
1
1825
by: David Resnick | last post by:
I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to the following code. The question is whether the problem is undefined behavior on the code's part or a compiler bug. #include <stdlib.h> #include <stdio.h> struct frob { char a; int b;
29
3322
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use 'atan2( )' Also, is there any function to get the round value, similar with floor and ceil, such like: round(3.1) = 3 round(3.6) = 4
7
1645
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data) Would this work? If so, then how to you load the data into data? Malloc?
9
2456
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
89
5968
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
26
2163
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work perfectly.)
7
318
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.