K
Keith Thompson
sandeep said:Keith said:sandeep said:Eric Sosman writes:
On 6/23/2010 3:13 PM, sandeep wrote:
Eric Sosman writes:
[...] (If your
perfectly valid K&R/C90/C99 code uses `class' or `private' as an
identifier, I think you're out of luck if someone decides to dump it
through a C++ compiler; this why I make a point of using `new' as an
identifier whenever it seems descriptive.)
Uhhh I think you mean of NOT using new, so that your code will be
portable to C++ compilers too.
No, "I meant what I said, and I said what I meant." I don't
want my C code misinterpreted subtly by a C++ compiler, because I
don't know C++ and even my best C probably has "mistakes" from a C++
point of view. By using `new' (and by not casting void* and so on), I
try to see to it that anyone so foolish as to use the wrong compiler
on my code will at least hear some warning bells.
But this is crazy!!
Why would you introduce non-portable constructions deliberately? I can
understand if there's some feature of C you need that clashes with C++
then use it, but why go out of your way to make your code non-portable
to C++ compilers?
He just explained his reasons. Did you not understand them, or do you
disagree with them? If you disagree, how was he wrong?
I disagree with Mr Sosman on this point.
In my opinion portability is a crucial feature of code. Time and time
again we see good code becoming obsoleted because it was written in a non-
portable way and hardware moves on. Sometimes non-portability is needed,
for example a network program will either use BSD sockets or WinSock and
so incompatability is forced. But in my opinion it is ONLY worth giving
up portability in order to get access to a useful platform-specific
feature.
Deliberately making code non-portable to C++ compilers for no clear
advantage is crazy! Many people choose to compile C with C++ compilers...
why make life difficult for them?
You might want to stop using the word "crazy" when talking to people who
know this stuff better than you do.
There is a clear advantage. It encourages users of his code to
compile it with a C compiler. Such code is portable to any system
with a C compiler.
For sure Mr Sosman knows C++ well enough! All that is needed is avoiding
certain artificial constructions that don't occur in normal code.
Eric says he doesn't know C++; do you have some reason to doubt
his word? The differences beween the C and the C-like subset of C++
are subtle. He chooses, quite reasonably, not to expend the effort
to keep track of those differences when he's writing C.
Can you list, off the top of your head, all the C++ keywords that
are legal identifiers in C? As of the 1998 or 2003 C++ standard,
there are 30 of them; as of the N2315 C++ draft, there are 33.
Can you use "long long" in C++? The answer depends on which version
of the standard your C++ compiler conforms to. What about the
"restrict" keyword?
Did you know that the syntax of the conditional operator differs
between C and C++? Do you know how it differs? Do you know how
that difference affects code? (I don't know the answer to that
last one myself, but I could figure it out.)
The C++ standard includes the most of the C standard library
by reference -- but which version, C90 or C99 (or C95 for that
matter)? Which version does the C++ compiler you're using support?
Which version is supported by the C++ compiler used by someone
you've never met who wants to compile your code three years from now?
I program in C++ for a living, and I'm not certain that I can write
C++-compatible C code with 100% reliability. I probably could if
I put some effort into it (including doing complete testing with
both C and C++ compilers), but it's just not worth it unless I have
a specific requirement to write cross-language compatible code.
If somebody else wants to take my or Eric's C code and turn it into
C++ code, they're welcome to do so, assuming the code is released
under some appropriate license. But it could be a non-trivial task.
Using "new" as an identifier doesn't make it significantly more
difficult, it just acts as a reminder.
It is a very heavy burden for someone with a C++ compiler at hand but no
C compiler!
Like who?
Most C++ compilers are also C compilers (or include C front-ends
in the same package); gcc is a prominent example, but certainly
not the only one.
Most compiler suites that handle both C and C++ will automatically
treat a file whose name ends in ".c" as C code.
If you want to compile C code, you need to get a C compiler.
The existence of a language that happens to be a near superset
of C does not impose any obligation on C programmers to write
code that's compatible with that language. Would you be upset
if I wrote C code that's deliberately imcompatible with Objective-C
(I'm not sure that's even possible)? Or with D?
Or do you advocate getting rid of the C language altogether, and
instead defining a restricted subset of C++? Isn't that the logical
conclusion of your argument?