This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: type based aliasing again - meaning of "undefined"


Michael Meissner wrote:
> 
> Also note the rules for ANSI C are different from ANSI C++ in terms of const.
> For example, I recall the following:
> 
>         int i;
>         *(int *)(const int *)&i = 1;
> 
> is legit under ISO C.  IIRC, this is explicitly mentioned in one of the TC's (I
> was still a member of the committee when TC.1 came out).

It's legit under C++ too; see section 7.1.5.1 of the standard. Modifying
an object through a pointer or reference to const (by casting away the
const) is undefined only if the pointed-to object was actually declared
const:

    int var(1);
    const int con(2);
    const int* cp1(&var);
    const int* cp2(&con);
    *const_cast<int*>(cp1) = 3; // OK, object is not really const
    *const_cast<int*>(cp2) = 4; // Undefined, object is really const

--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
  "There are many technical details that make Linux attractive to the
  sort of people to whom technical details are attractive."   -- Suck


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]