C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

unspecified behavior during construction of const object

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Yannick Moy
Guest





PostPosted: Thu Mar 03, 2005 8:56 pm    Post subject: unspecified behavior during construction of const object Reply with quote



The standard says in 12.1.15 that:

"During the construction of a const object, if the value of the object
or any of its subobjects is accessed though an lvalue that is not
obtained, directly or indirectly, from the constructor's this pointer,
the value of the object or subobject thus obtained is unspecified."

An example follows:

struct C;
void no_opt(C*);

struct C {
int c;
C() : c(0) { no_opt(this); }
};

const C cobj;

void no_opt(C* cptr) {
int i = cobj.c * 100; // value of cobj.c is unspecified
cptr->c = 1;
cout << cobj.c * 100 // value of cobj.c is unspecified
<< 'n';
}

I guess it has to do with putting the const object in read-only
memory, but what is the exact rationale behind this point ?
How is it implemented in compilers ?
Are there examples of people who suffered from not knowing this point
?

There is a discussion back on december 2003 (started the 17th) that
alludes to this point, but it does not answer my questions.

Thanks.

---
[ 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.jamesd.demon.co.uk/csc/faq.html ]

Back to top
msalters
Guest





PostPosted: Sat Mar 05, 2005 4:28 am    Post subject: Re: unspecified behavior during construction of const object Reply with quote




Yannick Moy wrote:
Quote:
The standard says in 12.1.15 that:

"During the construction of a const object, if the value of the
object
or any of its subobjects is accessed though an lvalue that is not
obtained, directly or indirectly, from the constructor's this
pointer,
the value of the object or subobject thus obtained is unspecified."

An example follows:

struct C;
void no_opt(C*);

struct C {
int c;
C() : c(0) { no_opt(this); }
};

const C cobj;

void no_opt(C* cptr) {
int i = cobj.c * 100; // value of cobj.c is unspecified
cptr->c = 1;
cout << cobj.c * 100 // value of cobj.c is unspecified
'n';
}

I guess it has to do with putting the const object in read-only
memory, but what is the exact rationale behind this point ?
How is it implemented in compilers ?

I think it's not only read-only memory. This allows compilers to
optimize more. In particular, a compiler can determine the possible
values of a const object if it can see all assignments in ctors.

E.g. in
void no_change( );

struct C {
int c;
C() : c(0) { no_change( ); }
};

the compiler can know that any const C object has c==0. There is
no legal way in which no_change can change c. It's similar to the
restrictions for const_cast. Also, the compiler might reorder the
assignment to members (possibly even in parallel with other code),
but not in this case. (because of non-const C objects).

Regards,
Michiel Salters

---
[ 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.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alberto Barbati
Guest





PostPosted: Sun Mar 06, 2005 6:28 pm    Post subject: Re: unspecified behavior during construction of const object Reply with quote



Yannick Moy wrote:
Quote:

const C cobj;

void no_opt(C* cptr) {
int i = cobj.c * 100; // value of cobj.c is unspecified
cptr->c = 1;
cout << cobj.c * 100 // value of cobj.c is unspecified
'n';
}

I guess it has to do with putting the const object in read-only
memory, but what is the exact rationale behind this point ?

I think this has little to do with read-only memory. Put yourself in the
compiler's shoes: it cannot nearly imagine that the line "cptr->c = 1;"
has a side-effect that modifies cobj, which has been declared const.
Therefore the compiler may decide to optimize the second "cobj.c"
sub-expression by reusing whatever cached value it may have from
evaluating the first one. In other words the code above may be
interpreted as

void no_opt(C* cptr) {
int i = cobj.c * 100;
cptr->c = 1;
cout << i << 'n';
}

and such an optimization would be perfecly legal. The fact that the
value of "i" is also unspecified is a different issue, but I think you
got the point.

Alberto

---
[ 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.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.