4 ms·
> it's really quite expressive, powerful, and orthogonal Expressive and powerful are definitely true, but I'm not so sure about 'orthogonal'. Most C++ feature
by tsimionescu 7y ago
> it's really quite expressive, powerful, and orthogonal
Expressive and powerful are definitely true, but I'm not so sure about 'orthogonal'.
Most C++ features seem to depend heavily on each other, and library decisions can impose restrictions/boilerplate on application code in many areas.
Especially as the language has evolved, features like exceptions, destructors, copy and move constructors, and operator overloading have become more and more indispensable. Template functions are still required for any kind of variance (e.g. you still can't have a non-template function that works on a const std::shared_ptr<const *base_type> or any derived type).
None of these is unbearable, but I do believe that even the modern subset of C++ is still the least orthogonal mainstream language in use (and definitely the most complicated by far). To be fair, it's also likely the most expressive mainstream language too, and still the most performant.
- mike_hock 7y ago> you still can't have a non-template function that works on a const std::shared_ptr<const *base_type> or any derived type I guess you meant, you can't have a function that accepts a shared_ptr to the base type without indirectly using template machinery? Because you can most definitely have a function that does this that is not a function template. #include <memory> #include <iostream> struct foo { virtual ~foo() = default; virtual void bark() const { std::cout << "fork\n"; } }; struct bar : foo { void bark() const override { std::cout << "bark\n"; } }; void poke_so_it_barks(std::shared_ptr<foo const> const thing) { thing->bark(); } int main() { poke_so_it_barks(std::make_shared<foo>()); poke_so_it_barks(std::make_shared<bar>()); }
- tsimionescu 7y agoOops, I picked a bad example it seems... Apparently `shared_ptr` does some magic to simulate covariance through type conversion. It still breaks in other cases (a function which returns a `shared_ptr<derived>` can't override a function which returns a `shared_ptr<base>`, unlike with derived* and base* ), but it still probably covers most real-world cases. If I had stuck with std::vector my point would have been better. Even though theoretically a `const std::vector<derived* >` IS-A `const std::vector<base* >`, that is, a read-only view of a collection of derived elements behaves the same as a read-only view of a collection of base elements, you can't pass the first to a function expecting the second in C++, unless you make your own function a template.
- umanwizard 7y agoHow could the type system reason about when covariance is valid?
- tsimionescu 7y agoIt could potentially use const-ness for these use cases, though I'm not sure that would be tractable. More realistically, it could allow the programmer to express it explicitly, as in C# (C<in T, out V> is covariant in T and contravariant in V) or Java (C<? extends T,? super V> is covariant in the first parameter, contravariant in the second). This would not solve the problem that the co-/contra-variance of some types may depend on their const-ness, but it would allow at least a covariant, always read-only, std::vector_view<T> (similar to IEnumerable<in T> in C#, which can also be implemented by the invariant List<T>/std::vector<T>).
- mike_hock 7y agoIn that case, you have a point. The closest you can get to covariance and contravariance is simulating it with implicit conversion operators/constructors (which then often need to be templates). But I don't see how this could be implemented without completely changing large parts of the language. In the one case of covariance that actually exists, overriding `virtual base* factory()` with `derived* factory() override`, the overridden function knows that the function it overrides returns a `base* `, so it can just return a `base* `, too, and callers through the subclass pointer know that they can adjust the return value back to a `derived* `. But even the closest cousin, argument contravariance (i.e. overriding `virtual foo(derived* )` with `foo(base* ) override`) doesn't seem feasible. Callers through the base pointer will pass a `derived* `, so the overridden function has to expect to be passed a `derived* `, and there is no adjustment that can be made to a `base* ` to turn it into a valid `derived* ` statically. The vector example is even more impossible. Since the function expects all members of the `std::vector<base* > const` argument to be `base* `, a caller with a `std::vector<derived* >` would have to allocate a new vector and adjust each individual pointer, which would be a terrible hidden runtime cost. (If they had added a constructor to accept a range to complement the one that takes an iterator pair, that's exactly what would have happened, though.)