Greg wrote:
Um, the interview seems neither inaccurate nor the least bit derogatory
toward C++; so it's hard to see how C++ the language is being
"impugned."
Okay, I'll go into more detail.
Templates in C++ can often replace C macros (look at std::min and
std::max). But of course doing so would only make sense if templates
were better than macros, which of course, they are.
Right, that's exactly the point. Templates are vastly more expressive
and useful than macros, so when the article states:
"C++ templates are really just like macros, except they look like
classes."
it's simply untrue. Shortly thereafter, he apparently contradicts
himself:
(1) "C# does the instantiation at runtime"
(2) "C# does strong type checking when you compile the generic type"
He's referring here to class instantiation, not object instantiation.
I don't see how you can claim to do strong type checking without
instantiating the generic. Maybe some parts of it are left for run
time, but his whole context here is about type-checking, so that's the
only part that's really relevant.
Anyway, he uses this as a basis for pointing out that, due to this
strong type checking at compile time, only valid operations for that
type are available at runtime. Well, yes, obviously -- otherwise, it
would fail the compile-time type checking. So again, I don't see how
he can claim that this is runtime instantiation. Maybe he's confusing
polymorphism with template instantiation or something?
He then goes on to use different wording to describe the fact that C++
also does strong type checking at compile time, but somehow decides
that the two languages are completely opposite in this regard:
"In C++, you can do anything you damn well please on a variable of a
type parameter type. But then once you instantiate it, it may not work,
and you'll get some cryptic error messages... So in a sense, C++
templates are actually untyped, or loosely typed. Whereas C# generics
are strongly typed."
It's a valid criticism that C++ template error messages are sometimes
verbose and difficult, but that's a red herring (besides, how do C#
error messages compare in similarly complex cases?). Both C++ and C#
provide compile-time type-checking of templates. If there is an error,
the compiler will report it. What he's doing here is interpreting this
behavior completely differently in each case. For C#, all he does is
talk up the wonderful guarantees provided by this checking, completely
glossing over the fact that, of course, C# must also generate error
messages if the type checking fails. For C++, he interprets the exact
same behavior as a failure, a complete lack of a type system. The
error messages are EXACTLY how you get strong type-checking! It's the
earliest possible opportunity, short of IDE integration, to check that
the operations performed on a parameterized type are valid. The
compiler will not let you proceed if you have type errors in your code
-- that IS strong type checking! His lack of understanding of the
terms he's using is further indicated by the phrase "in a sense" -- did
the definition of "strongly typed" suddenly become subjective? It's
the same as the obviously deceptive "from a certain point of view" sort
of reasoning.
It's absurd, it's false, it's misinformed, it's FUD. Engage your brain
and read it until you realize this.
Luke