Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this really legal? From "C++ templates"

Reply
Thread Tools

Is this really legal? From "C++ templates"

 
 
Vladimir Jovic
Guest
Posts: n/a
 
      04-14-2010
Hello,

Reading this book, I found this :

int f1( int const &r )
{
return ++(int&)r; // not reasonable, but legal
}

Is this really legal? Looks like an UB to me
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      04-19-2010
Stuart Golodetz wrote:

> On 14/04/2010 13:15, Daniel T. wrote:
>> Vladimir Jovic<(E-Mail Removed)> wrote:
>>
>>> Reading this book, I found this :
>>>
>>> int f1( int const&r )
>>> {
>>> return ++(int&)r; // not reasonable, but legal
>>> }
>>>
>>> Is this really legal? Looks like an UB to me

>>
>> int main() {
>> f1(5);
>> }
>>
>> I think you are right.

>
> That's like saying that this:
>
> struct X
> {
> void f() {}
> };
>
> void g(X *p)
> {
> p->f();
> }
>
> Exhibits undefined behaviour because you can do this:
>
> int main()
> {
> g(NULL);
> }
>
> ***
>
> In the original example, f1 has an implicit contract which says
> something like "r must not refer to something that is physically const".
> If you violate that contract, you end up with undefined behaviour. If
> you don't, you don't.


The prototype makes me think that r is not changed inside f1.
 
Reply With Quote
 
 
 
 
Keith H Duggar
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 9:22 am, "Daniel T." <(E-Mail Removed)> wrote:
> Michael Tsang <(E-Mail Removed)> wrote:
> > Stuart Golodetz wrote:
> > > On 14/04/2010 13:15, Daniel T. wrote:
> > >> Vladimir Jovic<(E-Mail Removed)> wrote:

>
> > >>> Reading this book, I found this :

>
> > >>> int f1( int const&r )
> > >>> {
> > >>> return ++(int&)r; // not reasonable, but legal
> > >>> }

>
> > >>> Is this really legal? Looks like an UB to me

>
> > The prototype makes me think that r is not changed inside f1.

>
> And as I understand it, the compiler is allowed to assume that r is not
> changed inside f1 when optimizing code that calls f1. So for example:
>
> void foo() {
> const int r = 12;
> f1(r);
> int w = r; // the compiler is allowed to assign the
> // value 12 to w here. Even though f1 changed r.


Walter Bright has posted a number of informative posts over the
years regarding this exact (incorrect) assumption. Here are the
general points:

1) it is legal to cast away const from a reference so a const &
is of little use to the compiler for optimization.

2) given 1) the compiler must analyze the code independent of
const & declarations.

3) the compiler can only sometimes determine legal optimizations
in the presence of references/pointers due to aliasing problems
etc. In the limited example above (with only that one reference)
it /would/ be able to do such optimizations but they would happen
regardless of the const qualifier.

4) top level const /is/ useful for optimization since it is UB to
modified a const objects (excepting mutable sub-objects). The most
common example are simple file or namespace scope constants such as:

int const TheBufferSize = 2048 ;

As to the topic of the thread, Leigh has already posted (twice)
the correct answer. Since many of you probably missed it due to
killfile filtering I've copied it below:

An adolescent in some killfiles wrote:
> It is UB if the actual object that r refers to is const.


KHD
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
really interesting... or really dull. (depends on your attitude) TrevorBoydSmith@gmail.com Java 2 09-01-2006 04:38 PM
OT : But help really really needed re: Domain Name selling, hosting etc. problem nc HTML 1 02-03-2005 07:24 PM
REALLY REALLY WERID PROBLEM!!!!pls take a look Amir ASP .Net 3 01-23-2004 06:01 PM
really really mysterious IE6 problem--secure site ultraviolet353 Computer Support 7 11-22-2003 07:56 PM
MR. ED REALLY, REALLY LOVES THE D60 !!! Annika1980 Digital Photography 9 10-28-2003 04:53 PM



Advertisments