James said:
James Kanze wrote:
[...]
template < typename T >
void reverse ( std::vector<T> & vec );
then you have to answer the question whether it should
match a word or not. Depending on the answer, inheritance
may or may not be the more correct way to go.
Conceptually, it shouldn't. It says it wants an
std::vector<T>. std::vector<T> is not some arbitrary
interface; it's a concrete class.
Yes, that's what the signature of the function says. The
question that one has to answer with regard to the type 'word'
is precisely whether a word _is_ a vector or not.
The standards committee has already defined what is or is not a
vector. If you add or remove functionality (or even if you
change the name), it is not an std::vector.
(Maybe. I'm really just speculating here. But I do believe
that the author of the class has the responsibility of defining
what that class is, i.e. its contract. What that means exactly,
in a case like this, is open to discussion; suppose I derive
just to provide a convenience constructor. Is the result an
std::vector or not?)
Assuming that deriving from std::vector is possible: The committee has
decided what a vector is, but only the author of 'word' can decide whether
a word is a vector (and in my view, that is shorthand for 'matches
functions with vector& arguments, has conversions vectors, ...'; stuff that
the language says about public derivation). I guess, the point is that I
would decide whether to publicly inherit or not just based upon whether the
technical consequences are desired or not.
Yes. The question is Socratic: designed to stimulate thought,
rather than have a concrete or precise answer.
That's because you're a software engineer, and not a (pure)
computer scientist
.
Actually, I am a mathematician (even worse); but right now, I am wearing my
programmers hat.
(In practice, of course, I can definitely think of cases where
I'd derive publicly from std::vector. My question is just to
what degree such derivation is a pragmatic compromise, rather
than something conform to some possibly overidealist theory.
Mental masturbation, I know.)
We definitely agree that public derivation from vectors can be justified by
pragmatic reasons, usually involving trade offs of some kind. I also would
contend that there are cases where _all_ consequences of public derivation
(in particular, bindings and conversions) are desired, so that no trade
offs are involved.
For the Platonic ideal, that probably is not enough as it depends on the
code base and in some way can change with time (of course, one could then
blame future changes instead of public derivation from containers). Now,
ultimately, I think all justifications in programming are pragmatic;
nonetheless there is a difference between justifying a trade off on one
hand and noting that _all_ consequences are desired on the other (even
though one falls a little short of an ideal). I guess, the platonic warning
is that: the consequences of public derivation depend on context and may
vary as the project evolves; in particular, undesired consequences can show
up at some later point. One should keep that in mind.
Best
Kai-Uwe Bux