------- Additional Comments From schlie at comcast dot net  2005-05-16 05:07 
-------
(In reply to comment #7)
> Subject: Re:  static_cast falsely allows const to be cast away
> That is your view.  However, not because GCC implements the ISO C++
> view of types, means that GCC has a narrow view of a type is.  I
> suspect that part of your speculation is based on unfamiliarity with
> both the C++ type system and the GCC internal notion of types.

- but apparently inadequate to express the necessary differentiation between
  constant and literal objects, as needed to be tracked by a compiler for
  these languages?

> | A literal string is not simply a 'const char [N]' object, as a
> | literal value may not be specified as a target of an assignment,
> | directly or indirectly though a pointer cast to a non-const object
> | reference, unlike as a plain old 'const' objects may be.
> 
> But, a plain old 'const' object cannot be a target of an assignment.
> There is no different there -- and certainly "static" does not imply a 
> difference there -- so the basis of your argument seems fragile to
> begin with.

- subtle possibly, but not fragile; the following simple program illustrates
  the problem, where if hypothetically 'literal' were a valid qualifier,
  then the problem would be easy to solve, and also flexibly enable the
  definition of functions which accept and return references to literals,
  as being distinct from const, where const means simply not writeable
  presently, not necessary never (i.e. can't ever assign to references,
  which is what literal semantics would seem to dictate.):

#include <stdio.h>

int main (void)
{
   // non-const pointers to literals should at least warn,
   // and assignments to literals should generate an error.
   char *cp = "(a)";         // compiles without warning/error (literal*)?
// ((char *)cp)[1] = 't';    // compiles without warning/error -> bus error!
   printf(cp);               // dies above if uncommented, otherwise "(a)"

   char ca[4] = "(b)";       // compiles without warning/error.
   ((char *)ca)[1] = 't';    // compiles without warning/error.
   printf(ca);               // outputs "(t)", as expected.
  
   // as above.
   const char *ccp = "(c)";  // compiles without warning/error (literal*)
// ((char *)ccp)[1] = 't';   // compiles without warning/error -> bus error!
   printf(ccp);              // dies above if uncommented, otherwise "(a)"

   const char cca[4] = "(d)";// compiles without warning/error.
   ((char *)cca)[1] = 't';   // compiles without warning/error.
   printf(cca);              // outputs "(t)", as expected.
  
   // as above.
   static const char sca[4] = "(e)"; // compiles w/o warning/error (literal*)
// ((char *)sca)[1] = 't';   // compiles without warning/error, (bus error)!
   printf(sca);              // dies above if uncommented, otherwise "(a)"

   // as above.
   "(f)";                    // compiles without warning/error (literal*)
// ((char *)"(f)")[1] = 't'; // compiles without warning/error -> bus error!
   printf("(f)");            // dies above if uncommented, otherwise "(a)"

   return 0;
}


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20475

Reply via email to