Topic: Explicitly declared const


Author: Bart van Ingen Schenau <bart@ingen.ddns.info>
Date: Tue, 31 Mar 2009 16:48:15 CST
Raw View
Ivan A. Kosarev wrote:

> Hi again,
>
> 3.5 #3 (of both the current IS and latest WD, N2857) speaks about
> objects explicitly declared const. If that's not just a typo, what are
> examples of those?
>
const float pi = 3.14f;

The object pi is explicitly declared const, because the const keyword is
specified in the declaration.
I am not aware of any method to implicitly declare an object as const,
so I can only assume that the word 'explicitly' is redundant.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Wed, 1 Apr 2009 17:08:40 CST
Raw View
On Apr 1, 12:48 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
> Ivan A. Kosarev wrote:

> > 3.5 #3 (of both the current IS and latest WD, N2857)
> > speaks about objects explicitly declared const. If
> > that's not just a typo, what are examples of those?

> const float pi = 3.14f;

> The object pi is explicitly declared const, because the
> const keyword is specified in the declaration.  I am not
> aware of any method to implicitly declare an object as
> const, so I can only assume that the word 'explicitly' is
> redundant.

I'm not sure, but what about things like:

     int const& ri = 42 ;

The compiler generates a temporary object; I'm not sure
whether that object is supposed to be const or not, but if
it is const, it is implicitly const, since there's no
definition of the object.  (It makes a difference whether it
is const.  Given the above, is "static_cast< int& >( ri ) =
0" legal?  I hope not.)

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Pavel Minaev <int19h@gmail.com>
Date: Fri, 3 Apr 2009 10:52:45 CST
Raw View
On Apr 1, 4:08 pm, James Kanze <james.ka...@gmail.com> wrote:
> I'm not sure, but what about things like:
>
>      int const& ri = 42 ;
>
> The compiler generates a temporary object; I'm not sure
> whether that object is supposed to be const or not

It quite obviously isn't, since you can happily call non-const member
functions on temporaries (remember the temp vector swap trick?). It is
an rvalue rather than lvalue, but that's another matter entirely.

I think that the only other way of getting a "true const" object is to
use new with a const-qualified type, i.e.:

   int const* pi = new int const(0);
   *const_cast<int*>(pi) = 1; // U.B.




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Balog Pal" <pasa@lib.hu>
Date: Fri, 3 Apr 2009 10:55:32 CST
Raw View
"James Kanze" <james.kanze@gmail.com>
> I'm not sure, but what about things like:
>
>     int const& ri = 42 ;
>
> The compiler generates a temporary object; I'm not sure
> whether that object is supposed to be const or not, but if
> it is const, it is implicitly const, since there's no
> definition of the object.  (It makes a difference whether it
> is const.  Given the above, is "static_cast< int& >( ri ) =
> 0" legal?  I hope not.)

std::string toString(int i);

std::string const & s = toString( 42 );

Here I'd think s is bound to an object that is not originally const. I
could
call  non-const member funcitons on toString( 42 ) directly, so why would
doing so later be so different.

Is that so?  Is the above example different?   I believe the standard
should
state explicitly, or if it can be deduced, at least add example to avoid
confusion.



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jiang <goo.mail01@yahoo.com>
Date: Sat, 4 Apr 2009 09:49:24 CST
Raw View
On Apr 2, 8:08 am, James Kanze <james.ka...@gmail.com> wrote:
> On Apr 1, 12:48 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
> wrote:
>
> > Ivan A. Kosarev wrote:
> > > 3.5 #3 (of both the current IS and latest WD, N2857)
> > > speaks about objects explicitly declared const. If
> > > that's not just a typo, what are examples of those?
> > const float pi = 3.14f;
> > The object pi is explicitly declared const, because the
> > const keyword is specified in the declaration.  I am not
> > aware of any method to implicitly declare an object as
> > const, so I can only assume that the word 'explicitly' is
> > redundant.
>
> I'm not sure, but what about things like:
>
>      int const& ri = 42 ;
>
> The compiler generates a temporary object; I'm not sure
> whether that object is supposed to be const or not,

It should be const, since in 8.5.3/p5, we have
      [...]
    Otherwise, a temporary of type   cv1 T1   is created and
    initialized from the initializer expression using the rules
    for a non-reference copy initialization (8.5).
    The reference is then bound to the temporary.
      [...]

> but if it is const, it is implicitly const, since there's no
> definition of the object.

Yes, well, if the compiler generated const is called
"implicitly const".

> (It makes a difference whether it
> is const.  Given the above, is "static_cast< int& >( ri ) =
> 0" legal?  I hope not.)

For the same reason, I do not believe the above cast is valid.
We can not cast a const int to int& in my mind.

For the OP's question, I think the word "explicit" can be
removed without worrying about the definition of "internal
linkage". The same rule is rephased in 7.1.6.1/p2 and
no "explicit" word used there.

Regards,

Jiang


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Sat, 4 Apr 2009 19:56:09 CST
Raw View
On Apr 3, 6:52 pm, Pavel Minaev <int...@gmail.com> wrote:
> On Apr 1, 4:08 pm, James Kanze <james.ka...@gmail.com> wrote:

> > I'm not sure, but what about things like:

> >      int const& ri = 42 ;

> > The compiler generates a temporary object; I'm not sure
> > whether that object is supposed to be const or not

> It quite obviously isn't, since you can happily call non-const
> member functions on temporaries (remember the temp vector swap
> trick?). It is an rvalue rather than lvalue, but that's
> another matter entirely.

You can only call non-const member functions on non-const
rvalues.  That was (part of) the point of my question: is the
temporary here const or not?

> I think that the only other way of getting a "true const"
> object is to use new with a const-qualified type, i.e.:

>    int const* pi = new int const(0);
>    *const_cast<int*>(pi) = 1; // U.B.

Any object declared const is really const.  For class types, the
const of a return value is significant.  For non-class types,
it's ignored, because they aren't objects (and have no member
functions which can be called on them).  None of which is really
relevant here: the compiler has created a temporary object, not
the programmer.  Is that object const or not?

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Sat, 4 Apr 2009 19:56:32 CST
Raw View
On Apr 3, 6:55 pm, "Balog Pal" <p...@lib.hu> wrote:
> "James Kanze" <james.ka...@gmail.com>

> > I'm not sure, but what about things like:

> >     int const& ri = 42 ;

> > The compiler generates a temporary object; I'm not sure
> > whether that object is supposed to be const or not, but if
> > it is const, it is implicitly const, since there's no
> > definition of the object.  (It makes a difference whether it
> > is const.  Given the above, is "static_cast< int& >( ri ) =
> > 0" legal?  I hope not.)

> std::string toString(int i);

> std::string const & s = toString( 42 );

> Here I'd think s is bound to an object that is not originally
> const. I could call  non-const member funcitons on toString(
> 42 ) directly, so why would doing so later be so different.

> Is that so?  Is the above example different?   I believe the
> standard should state explicitly, or if it can be deduced, at
> least add example to avoid confusion.

I agree.  I seem to recall wording to the effect that attempting
to modify the temporary object is undefined behavior, but I
can't find it.  Still, I think it would be a rather horrible
thing to support.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Pavel Minaev <int19h@gmail.com>
Date: Sun, 5 Apr 2009 03:08:51 CST
Raw View
On Apr 4, 6:56 pm, James Kanze <james.ka...@gmail.com> wrote:
> > std::string toString(int i);
> > std::string const & s = toString( 42 );
> > Here I'd think s is bound to an object that is not originally
> > const. I could call  non-const member funcitons on toString(
> > 42 ) directly, so why would doing so later be so different.
> > Is that so?  Is the above example different?   I believe the
> > standard should state explicitly, or if it can be deduced, at
> > least add example to avoid confusion.
>
> I agree.  I seem to recall wording to the effect that attempting
> to modify the temporary object is undefined behavior, but I
> can't find it.  Still, I think it would be a rather horrible
> thing to support.

If there was such a wording applied to a general case (and not the
specific one here, where the temporary is bound to a reference-to-
const - as Jiang pointed out, that case is definitely covered by the
Standard, and I was wrong there), most of existing C++ code would
suddenly become U.B. Such as the one that I've pointed earlier:

   std::vector<int> v;
   ...
   std::vector<int>(v).swap(v);

which is a very common idiom.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Ivan A. Kosarev" <ik@unicals.com>
Date: Mon, 30 Mar 2009 18:39:45 CST
Raw View
Hi again,

3.5 #3 (of both the current IS and latest WD, N2857) speaks about
objects explicitly declared const. If that's not just a typo, what are
examples of those?

--

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]