David Gallay
David Gallay

Reputation: 61

Virtual parent for containers and 'iterable' class

I was wondering if there was a reason why there's no virtual parent inheritance for all std containers.

Since C++ is oop, I get some reason for using external function as such from algorithm header.

But i wonder why string does not have size() function ( doing the same as length ) for genericity?

Also, why is there no "iterable" class from which would iterables containers inherit ( because of the different types of iterator? ) This one come from my short experience with python. In my opinion, python's biggest strengh is how easy if is to use iterable.

EDIT: To clarify myself: i know C++ is mutli-paradigm, but i would like to focus on containers. We could have keep C-style using struct and functions, why defining some as methods and others not? For encapsulation only? Having both sequential and oop means is confusing and illogical i think Why having begin(vec) and vec.begin() ?

And i speak about iterable, not iterators, and yes, iterator was a class in the std defining an iterface for iterator which is now deprecated ( and i understand why ). So iterators are object, why not doing iterable object ( juste to pack iterator begin and iterator end together if it is not to define virtual parent for iterable containers) ?

I hope i was clearer this time. I already got answer i thought about by myself for the most, but i would prefer an explanation about the current logic in the std.

Thank you for the time taken to read and answer.

Upvotes: 1

Views: 94

Answers (3)

Yksisarvinen
Yksisarvinen

Reputation: 22211

I was wondering if there was a reason why there's no virtual parent inheritance for all std containers.

Design decision at early stages of C++ standard forming. There is no much reason to do that - sure, you would have a nice common interface, but this can be also achieved with consistent standard managing (all containers already do have common set of functions without an explicit interface).
A disadvantage of a common base is that all containers would suddenly need virtual functions, and virtual functions are not free (more expensive at runtime than normal functions).

Since C++ is oop

It's not. C++ is a multiparadigm language. For example, you can have functions outside of any class (unlike Java, which is following OOP paradigm more strictly).

i get some reason for using external function as such from algorithm header.

A follow-up decision after the above. Having global functions accepting generic pair(s) of iterators allows to use any container (even user-defined) with these functions.

But i wonder why string does not have size() function ( doing the same as length ) for genericity?

It does have such a function and it always had. length() is another option, provided for better readability - it's more "English" to ask about length of string than to ask about size of string

Also, why is there no "iterable" class from which would iterables containers inherit ( because of the different types of iterator? ) This one come from my short experience with python. In my opinion, python's biggest strengh is how easy if is to use iterable.

There is one very good reason for that - iterators don't have to be of class type. Pointer type satisfies all of the requirements for RandomAccessIterator, but it is not a class.

Upvotes: 4

nromashchenko
nromashchenko

Reputation: 111

C++ prefers compile-time polymorphism, suggesting you to use templates instead of virtual calls. Back in the good old days, it was more efficient (virtual calls are still not free). There is however a certain level of standardization for containers: all of them support iterators, and standardized C++20 ranges are coming as well.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

I was wondering if there was a reason why there's no virtual parent inheritance for all std containers.

There is a cost attached to this (virtual dispatch is not free!), and most people (all people?) wouldn't need it.

In C++, you don't pay for what you don't use. (In theory.)

i wonder why string does not have size() function ( doing the same as length ) for genericity?

It does.

why is there no "iterable" class from which would iterables containers inherit ( because of the different types of iterator? )

Because we don't need one. You claim that C++ is "oop", but that is not true: it is multi-paradigm, and most of the standard library (particularly the parts inherited from the antique STL) are template based, not object-oriented.

This allows us to provide iterators that fit a set of "requirements", rather than those that fit some definition of "class". For example, pointers (which do not have class type!) are a valid kind of iterator.

This one come from my short experience with python. In my opinion, python's biggest strengh is how easy if is to use iterable.

That's reasonable. But C++ is not Python.

Upvotes: 4

Related Questions