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


Groups > comp.std.c++ > #394

Re: Definition of const object

From Daniel Krügler <daniel.kruegler@googlemail.com>
Newsgroups comp.std.c++
Subject Re: Definition of const object
Date 2011-12-09 23:54 -0800
Organization A noiseless patient Spider
Message-ID <jbsi4v$esj$1@dont-email.me> (permalink)
References <14174863.133.1323173824046.JavaMail.geo-discussion-forums@vbaa2>

Show all headers | View raw


On 2011-12-06 22:52, Roman Perepelitsa wrote:
>
> The standard mentions that modifying a const object results in
> undefined behavior, but I couldn't find a definition of const object.


There does exist a definition for a const object in [basic.type.qualifier] p1:

"[..] The term object type (1.8) includes the cv-qualifiers specified
when the object is created. The presence of a const specifier in a
decl-specifier-seq declares an object of const-qualified object type;
such object is called a const object.[..]"

> Can a heap-allocated object be a const object?


Good question. The definition above seems not to cover this case.

> #include<iostream>
>
> int main() {
>    const int* p = new const int(42);
>    // Modifying a const objects is undefined behavior.
>    // Does p point to a const object?
>    *const_cast<int*>(p) = 24;
>    // Is it required to print 24?
>    std::cout<<   *p<<   std::endl;
> }


It is hard to say whether this is well-defined, but I wouldn't rely on that;-)

I have forwarded your question to the CWG.

HTH & Greetings from Bremen,

Daniel Krügler




--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Find similar


Thread

Definition of const object Roman Perepelitsa<Roman.Perepelitsa@gmail.com> - 2011-12-06 13:52 -0800
  Re: Definition of const object Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-12-09 23:54 -0800

csiph-web