Why C ++ containers do not implement interfaces

Possible duplicate:
Why is C ++ STL so template dependent so much? (not on interfaces)

Why stl and Qt containers do not implement interfaces. For example, for vectors and lists it must be Enumerable .

Like this:

 template <typename T> class Enumerable { public: virtual const T at(int k) = 0; //.... virtual ~Enumerable() {} }; template <typename T> class Vector: public Enumerable<T> { public: virtual const T at(int k); //.... }; 

As a result, the code that I use forces me to use the specific types of containers that are used in it.

+4
source share
4 answers

What are you trying to achieve, what do you think you cannot do with standard containers? The answer to your question is that they don’t need it. Using templates, you have all the advantages of interfaces, you will get zero execution cost.

+3
source

The STL (standard template library) design does not require the usual virtual functions. When the STL was developed, the cost of virtual functions was significant enough to try to avoid them in critical parts of the code. General programming allows this using only specific types. It explains http://en.wikipedia.org/wiki/Standard_Template_Library

+1
source

Well, I get a vector from somewhere, and I need to give a list the same data somewhere. And now I need to convert vector to list to O (n)

No problem, the list already has a range constructor:

 std::list<your_element_type_here> yay(your_vector.begin(), your_vector.end()); 
0
source

STL usually avoids the use of virtual functions and similar abstractions. The concept of Enumerable modeled by the concept of an iterator, and iterators use template abstractions. If you want virtual abstractions for containers, look at the Thomas Becker any_iterator header.

0
source

Source: https://habr.com/ru/post/1383289/


All Articles