Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Bo Persson" Newsgroups: comp.lang.c++ Subject: Re: removing constness of a true const Date: Sat, 16 Jul 2011 08:32:10 +0200 Lines: 41 Message-ID: <98cpj0FmsU1@mid.individual.net> References: <1e55c267-2789-498c-a5a1-f56e499336e5@z12g2000yqj.googlegroups.com> <47b1098f-e550-424b-acd3-a26d2e419b4d@12g2000yqr.googlegroups.com> <9895ljF70aU1@mid.individual.net> X-Trace: individual.net zCIA6mgs+npLqFOI+CEZiwTMBOC2Yxw9k5QqcW8A227r+BQSVX Cancel-Lock: sha1:feXnDJ+PQRPF7CQCeE+a/ZSYByM= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109 X-RFC2646: Format=Flowed; Response X-Antivirus: avast! (VPS 110715-1, 2011-07-15), Outbound message X-Antivirus-Status: Clean Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:7863 Victor Bazarov wrote: > On 7/14/2011 5:33 PM, Bo Persson wrote: >> siddhu wrote: >>> On Jul 14, 1:05 pm, "Balog Pal" wrote: >>>> "siddhu" >>>> >>>>> I guess removing constness of a true const( I guess it is also >>>>> called bitwise const ) using const_cast is undefined behavior. >>>>> Can you please give an example of a true const and may be a >>>>> sample code which exhibits undefined behavior by removing >>>>> constness of a true const. >>>> >>>> Removing constness is well defined. But actually modifying >>>> non-mutable member of a const object is UB. By any means, >>>> including the use of pointer/ref from const_cast. >>> >>> Thanks Balog! >>> Does that mean removing constness of 'this' in const member >>> function is an UB? >> >> It is if the object is really const. >> >> Suppose the data is stored in a ROM. What happens when you try to >> change it? > > I see two concepts here, removing of 'const' and changing. I don't > think that the former is the same as the latter, or is it? > > V Right, casting away const as such is ok, as long as you don't use it for anything non-const. My point was more that class objects can very well be read-only. Trying to change them will then have an undefined result. Bo Persson