This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Compiling GCC With a C++ Compiler (g++)
Zack Weinberg <zack@codesourcery.com> writes:
| Gabriel Dos Reis <gdr@cs.tamu.edu> writes:
|
| > Geoffrey Keating <geoffk@apple.com> writes:
| > | The 'const' is there to indicate that the string should not be changed
| > | once the STRING_CST is created;
| >
| > And how do you create it?
|
| By casting away the const, as is done in build_string. Or by
| constructing the data in memory before giving it the type with the
| const qualifier.
In the message I sent, I listed:
Because, if you mark a data member as const, then you have to
initialize it somehow at object contruction time -- either through
brace-enclosed initializer or real constructors. But, now, you go on
putting it in a union, and that union does not even happen to be the
first member.
Constructing the data in memory before giving it the type with the const
qualifier, is morally what C++ constructors do.
The code in build_string
memset (s, 0, sizeof (struct tree_common));
TREE_SET_CODE (s, STRING_CST);
TREE_STRING_LENGTH (s) = len;
memcpy ((char *) TREE_STRING_POINTER (s), str, len);
((char *) TREE_STRING_POINTER (s))[len] = '\0';
is trying to do similar thing, but it is undefined behaviour.
[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.
| How else do you propose to enforce write-once semantics on a data
| structure that is, in fact, write-once?
Notice that the declaration was "const char *pointer" before Geoff's
change.
| I am definitely with Geoff here - this should be allowed (or, to
I don't think this is an issue of being "with somebody" as opposed to
writing a code with defined meaning.
| rephrase, if C++ has no construct which allows what we want to do
| here, that is a language deficiency).
C++ allows several ways out of this, some may or may not be applicable
to C. Definitely, the const-casting away is undefined behaviour.
My own rule of thumb is that we one starts fiddling with bytes and
type punning and trickery, something is wrong it is time to think at
least twice about it.
-- Gaby