<snip description of working with C and C++ code close together>
Not sure there is an easy way around that. If you're writing code that
you expect to be compiled as C and as C++, I suppose you just have to
not do that. Doesn't C99 have a built in boolean type now?
C99 has a built in boolean type (_Bool), as well as a standard header
(<stdbool.h>) that defines macros for bool, true, and false.
Since in practice you're unlikely to be able to avoid having to coexist
with C90 for quite some time, this doesn't make it any easier to use
bool in headers that have to be both C- and C++-clean. It's probably
still best to use int and document that the value represents a boolean
value for that.
The new C++ casts are preferred for two reasons. They are easier to
spot, both by humans and by search tools. And, probably more
importantly, all apart from reinterpret_cast are less powerful than
C-style casts, allowing the compiler to still do a certain amount of
type checking.
Also useful is that they do better with indicating why a cast is used.
F'rexample:
free((void *)temp);
Is the programmer not aware that conversions to void * don't need
a cast? Is temp const- or volatile-qualified for some reason? Is temp
a non-pointer type that has had a pointer stuffed in it?
Now answer the same question for:
free(const_cast<void *>(temp));
(I've encountered this in testing code that used a volatile-qualified
pointer obtained from malloc to avoid having some timing tests optimized
out. Perhaps not the best example, since the total amount of code for
such things tends to be small enough that it's easy to find supporting
evidence for "casting away qualifiers", but still enough to demonstrate
the point.)
I have the impression that well written C code is likely to contain
more casts (particularly pointer casts) than well written C++ code.
But I am not a C programmer so I may have got that idea simply from
seeing code from people who aren't as good as they think they are.
Most likely the latter.
There are a very small number of cases where a cast is required, but they
tend not to be the places most programmers use them. (In my experience,
the most important use of casts is to shut up compiler warnings for
conversions that can lose information, like double->float.) Pointer
casts especially are almost always a sign that the code needs a closer
look and probably some debugging.
C tends to use fewer pointer conversions than C++ in the first place,
and the most common one (void * returned from malloc -> pointer used
to point to stuff -> void * passed to free) is done without a cast.
(Note that void * has a somewhat different purpose in C than in C++,
and corresponding differences in rules for its use.)
(For the curious, the places where pointer casts are actually valid and
useful in C (that I know of) are:
-Passing pointers as variadic arguments where the type of the pointer
isn't the expected type. The canonical example of this is printf's
"%p", which expects a void *, and there isn't enough type information
to allow an automatic conversion.
(Not as relevant in C++, since there's usually a better way to do
things than using variadic functions)
-Casting away const for functions that return a pointer to the same
thing as a pointer they're passed, without modifying what's pointed
at (several <string.h> functions do this or the moral equivalent).
(Not useful in C++, because of stronger const rules and overloading:
C's "char *return_pointer_into_string(const char *)" would have two
versions in C++, one taking and returning char * and one taking and
returning const char *)
-Taking advantage of the rule that a pointer to a struct, suitably
converted, can be used as a pointer to its first member. This usually
means the programmer is doing something odd anyways.
(Valid (as far as I know) and potentially useful in C++, but most
often used to allow a limited form of polymorphism that in C++ is
better done using other language features)
-Converting a pointer to char * to examine the representation of an object
(Just as valid, and just as useful (that is, "not portably"), in C++
as in C)
If you really want to know all the details, ask in comp.lang.c .)
dave