Zack Weinberg wrote:
On Mon, 2005-05-23 at 01:15 -0500, Gabriel Dos Reis wrote:

Hi,

 I spent the week-end trying to get GCC -- mainline -- compilable
(i.e. those compoenents written in C) with a C++ compiler (e.g. g++).


These results are very interesting.

As a general observation: A lot of the things you have found to be
problematic, are in fact preferred idioms for C code.  For instance,
no standard-C programmer would ever write an explicit cast on malloc's
return value.

I don't know that I agree; I used to write such casts on other projects. I learned C and C++ at the same time, and did a fair amount of work with formal type systems, though, so I may not be representative.

is correct C++.  Furthermore, as I've said before, I support migrating
to C++ -- but only if the C++ ABI and libstdc++ soname are first
permanently frozen.  If we do not do that first, we risk being trapped
into a situation where we need specific versions of GCC to compile
specific newer versions of GCC, which would be a Bad Thing.

Line Daniel, I'm confused by this. I think we would need to be sure of source compatibility with a reasonably large set of compilers, so I'd say that it would be bad to depend on (say) a fix for a bug with templates that was fixed in the last release. But, it's doable to specify a super-conservative subset of C++ that works with anything that vaguely smells like a C++ compiler. I'd certainly say that the common subset of C and C++ is such a subset.

Being allowed to do this is very important.  Some enumerated types are
to be treated as opaque outside a very narrow context; the only way to
do that in C is to have (a typedef of) unsigned int as the visible type,
and only declare the enumerated type in the context where it's allowed
to be used.

I agree with the goal of more hiding.

You can do this in C by using an incomplete structure type in most places, and then, in the files where you want the definition visible, defining the structure to have a single field of the enumerated type. That is a little messy, but it is C++-compatible. (In fact, in ISO C++, without the additions presently in the WP, you can't do better; forward declarations of enums are still not allowed.)

(The other thing you can do is to depend on the fact that enums and unsigned ints have the same representation, which you can force in C++ by declaring extra enumeration constants of values like UINT_MAX, and then use explicit casts at places where you want to go back and forth. I think this is not as nice as the incomplete structure approach.)

--
Mark Mitchell
CodeSourcery, LLC
[EMAIL PROTECTED]
(916) 791-8304

Reply via email to