Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7837

Re: removing constness of a true const

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: removing constness of a true const
Date 2011-07-15 09:54 -0400
Organization A noiseless patient Spider
Message-ID <ivpgr1$m32$1@dont-email.me> (permalink)
References <1e55c267-2789-498c-a5a1-f56e499336e5@z12g2000yqj.googlegroups.com> <ivpdnr$okd$1@news.albasani.net>

Show all headers | View raw


On 7/15/2011 9:02 AM, Johannes Schaub (litb) wrote:
> siddhu wrote:
>
>> 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.
>
> Some folks here mentioned literals like double or int literals. But those
> are not const, and const cannot meaningfully applied to them.

If the code doesn't know it's a literal, does 'const' apply then?

   void foo(const int& i)
   {
      // is 'i' const here?  Can we cast away its constness?
      int& ri = const_cast<int&>(i);  // is that UB?
   }

   int main()
   {
      foo(42);
      int a = 42;
      foo(a);
   }

A literal is not a run-time object, of course.  It's an element of the 
program (before it's compiled and run).

Inside 'foo' the code doesn't really know what 'i' represents.  It's 
bound to something.  To what?  Why should the code care?

That's what I thought siddhu was asking about.

> What "const" means is that the stored value of an object does not change
> from one to another value. Changing the object may change the stored value
> to something else. "const" prevents such changes to the object.

'const' is a keyword.  It means different things in different contexts.

> A value in and of itself does not convey a meaning of "const". Hence, in
> C++, non-class and non-array rvalues cannot be const qualified. That makes a
> great deal of sense, because they are not objects.
>
> It should be mentioned that the C++ Standard does not know a strict
> distinction between "value" and "object". In my text above, I used "value"
> in the sense of "the abstract meaning designated by an object's value
> representation", as defined by 3.9p4 (sadly that definition of "value" isn't
> used consistently throughout the spec or draft).
>

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

removing constness of a true const siddhu <siddharth.sng@gmail.com> - 2011-07-14 08:07 -0700
  Re: removing constness of a true const Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-14 11:55 -0400
    Re: removing constness of a true const siddhu <siddharth.sng@gmail.com> - 2011-07-14 09:25 -0700
      Re: removing constness of a true const Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-14 16:18 -0400
        Re: removing constness of a true const "Balog Pal" <pasa@lib.hu> - 2011-07-15 01:18 +0200
          Re: removing constness of a true const Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-15 08:17 -0400
            Re: removing constness of a true const "Balog Pal" <pasa@lib.hu> - 2011-07-15 18:04 +0200
              Re: removing constness of a true const Ian Collins <ian-news@hotmail.com> - 2011-07-16 08:36 +1200
                Re: removing constness of a true const "Balog Pal" <pasa@lib.hu> - 2011-07-16 02:00 +0200
            Re: removing constness of a true const Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-15 16:08 -0500
              Re: removing constness of a true const Ian Collins <ian-news@hotmail.com> - 2011-07-16 09:30 +1200
  Re: removing constness of a true const "Balog Pal" <pasa@lib.hu> - 2011-07-14 19:05 +0200
    Re: removing constness of a true const siddhu <siddharth.sng@gmail.com> - 2011-07-14 12:05 -0700
      Re: removing constness of a true const "Bo Persson" <bop@gmb.dk> - 2011-07-14 23:33 +0200
        Re: removing constness of a true const Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-14 17:46 -0400
          Re: removing constness of a true const "Bo Persson" <bop@gmb.dk> - 2011-07-16 08:32 +0200
  Re: removing constness of a true const "Johannes Schaub (litb)" <schaub.johannes@googlemail.com> - 2011-07-15 15:02 +0200
    Re: removing constness of a true const Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-15 09:54 -0400

csiph-web