Is this really legal? From "C++ templates"

Discussion in 'C++' started by Vladimir Jovic, Apr 14, 2010.

  1. Hello,

    Reading this book, I found this :

    int f1( int const &r )
    {
    return ++(int&)r; // not reasonable, but legal
    }

    Is this really legal? Looks like an UB to me
     
    Vladimir Jovic, Apr 14, 2010
    #1
    1. Advertising

  2. Stuart Golodetz wrote:

    > On 14/04/2010 13:15, Daniel T. wrote:
    >> Vladimir Jovic<> wrote:
    >>
    >>> Reading this book, I found this :
    >>>
    >>> int f1( int const&r )
    >>> {
    >>> return ++(int&)r; // not reasonable, but legal
    >>> }
    >>>
    >>> Is this really legal? Looks like an UB to me

    >>
    >> int main() {
    >> f1(5);
    >> }
    >>
    >> I think you are right.

    >
    > That's like saying that this:
    >
    > struct X
    > {
    > void f() {}
    > };
    >
    > void g(X *p)
    > {
    > p->f();
    > }
    >
    > Exhibits undefined behaviour because you can do this:
    >
    > int main()
    > {
    > g(NULL);
    > }
    >
    > ***
    >
    > In the original example, f1 has an implicit contract which says
    > something like "r must not refer to something that is physically const".
    > If you violate that contract, you end up with undefined behaviour. If
    > you don't, you don't.


    The prototype makes me think that r is not changed inside f1.
     
    Michael Tsang, Apr 19, 2010
    #2
    1. Advertising

  3. On Apr 19, 9:22 am, "Daniel T." <> wrote:
    > Michael Tsang <> wrote:
    > > Stuart Golodetz wrote:
    > > > On 14/04/2010 13:15, Daniel T. wrote:
    > > >> Vladimir Jovic<> wrote:

    >
    > > >>> Reading this book, I found this :

    >
    > > >>> int f1( int const&r )
    > > >>> {
    > > >>> return ++(int&)r; // not reasonable, but legal
    > > >>> }

    >
    > > >>> Is this really legal? Looks like an UB to me

    >
    > > The prototype makes me think that r is not changed inside f1.

    >
    > And as I understand it, the compiler is allowed to assume that r is not
    > changed inside f1 when optimizing code that calls f1. So for example:
    >
    > void foo() {
    > const int r = 12;
    > f1(r);
    > int w = r; // the compiler is allowed to assign the
    > // value 12 to w here. Even though f1 changed r.


    Walter Bright has posted a number of informative posts over the
    years regarding this exact (incorrect) assumption. Here are the
    general points:

    1) it is legal to cast away const from a reference so a const &
    is of little use to the compiler for optimization.

    2) given 1) the compiler must analyze the code independent of
    const & declarations.

    3) the compiler can only sometimes determine legal optimizations
    in the presence of references/pointers due to aliasing problems
    etc. In the limited example above (with only that one reference)
    it /would/ be able to do such optimizations but they would happen
    regardless of the const qualifier.

    4) top level const /is/ useful for optimization since it is UB to
    modified a const objects (excepting mutable sub-objects). The most
    common example are simple file or namespace scope constants such as:

    int const TheBufferSize = 2048 ;

    As to the topic of the thread, Leigh has already posted (twice)
    the correct answer. Since many of you probably missed it due to
    killfile filtering I've copied it below:

    An adolescent in some killfiles wrote:
    > It is UB if the actual object that r refers to is const.


    KHD
     
    Keith H Duggar, Apr 19, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    477
  2. Marco Jez

    [templates] is this code legal?

    Marco Jez, Sep 18, 2005, in forum: C++
    Replies:
    9
    Views:
    366
    Marco Jez
    Sep 19, 2005
  3. Mark P

    Is this legal (templates)

    Mark P, Apr 5, 2006, in forum: C++
    Replies:
    9
    Views:
    330
    Mark P
    Apr 6, 2006
  4. recover
    Replies:
    2
    Views:
    809
    recover
    Jul 25, 2006
  5. Replies:
    4
    Views:
    450
    Andrey Tarasevich
    Jul 20, 2010
Loading...

Share This Page