4

Possible Duplicate:
Why is the C++ STL is so heavily based on templates? (and not on interfaces)

Why doesn't neither stlnor Qt containers implement interfaces. For example, it colud be Enumerable for vectors and lists.

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, code that I use, forces me to use concrete types of containers, that are used in it.

Community
  • 1
  • 1
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • 14
    Because you're Thinking About It Wrong. Use templates to write generic code; there's no need to add an expensive virtual hierarchy in order to impose a forced and unnecessary "everything must be an object" pseudo-philosophy. – Kerrek SB Nov 27 '11 at 12:21
  • First, not Enumerable but RandomAccessContainer. Second, if you want only use an abstract base, will a template adapter be enough? – Arpegius Nov 27 '11 at 12:42
  • Yes, you are right. It has some limitations, templates cannot be compiled to library, that's why I think this was not done in the particular code, but this case in uncommon enough not to care about it, I think – Lol4t0 Nov 27 '11 at 12:51
  • @Kerrek SB: That should've been an answer instead of a comment, I think. – Frerich Raabe Nov 27 '11 at 13:17

4 Answers4

3

What is it that you are trying to achieve that you think you cannot do with the standard containers? The answer to your question is that they don't need to. Using templates you have all the advantages interfaces would bring at zero run-time cost.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • Well, I obtain `vector` from somewhere and I need to give `list` of the same data to somewhere. And now I need to convert `vector` to `list` in O(n) in Qt. With `Enumerable` I would obtain `Enumerable*` from somewhere and give `Enumerable&` to somewhere without any conversion – Lol4t0 Nov 27 '11 at 12:27
  • 1
    @Lol4t0: How often do you need a concrete container of a specific type, rather than an iterator? While it's true that such polymorphism allows abstracting over container types, so do `template`'d functions working on iterators. –  Nov 27 '11 at 12:31
  • @Lol4t0 The solution you're thinking about might not be the best. Vectors and lists are very different conceptually, so there's a good chance you're not using the best approach. Either start directly with the type of container you're going to need, or (as already suggested) work with templates and iterators. – Paul Manta Nov 27 '11 at 12:36
  • Oh, you are right, the problem in code, that I could use. It's not enough generic. – Lol4t0 Nov 27 '11 at 12:38
1

The design of the STL (Standard Template Library) is to not requires the usual of virtual functions. When the STL was designed, virtual functions cost was significant enough to try to avoid them in critical parts of the code. Generic programming allow this, by using only concrete types. It's explained here http://en.wikipedia.org/wiki/Standard_Template_Library

Monkey
  • 1,838
  • 1
  • 17
  • 24
  • Even with low cost of virtual functions today, because the function is virtual you usually wouldn't be able to inline the calls to those functions which would degrade performance for significant uses cases of containers code. – Ghita Oct 12 '12 at 06:04
0

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

No problem, list already has a range constructor:

std::list<your_element_type_here> yay(your_vector.begin(), your_vector.end());
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 1
    Don't you think, that it will iterate from `begin` to `end` and copy values one by one to list? It is O(n) – Lol4t0 Nov 27 '11 at 12:43
  • 2
    I don't get it, O(n) is exactly what you asked for, isn't it? – fredoverflow Nov 27 '11 at 13:16
  • 5
    @Lol4t0: Your consumer interface is broken. Instead of requiring a `std::list`, it should also accept a pair of (templated) iterators. Generally, C++ objects *own* their contents, and if you don't want to create an owning copy, you should permit direct access to another containers data via iterators. – Kerrek SB Nov 27 '11 at 13:26
  • @Kerrek SB, exactly! I've already understood this and explained in comment to topic. Thank you, anyway – Lol4t0 Nov 27 '11 at 13:33
0

The STL generally avoids the use of virtual functions and similar abstractions. The concept of an Enumerable is modelled by the concept of an iterator, and iterators use template abstractions. If you want virtual abstractions for containers, have a look at Thomas Becker's any_iterator header.

thiton
  • 35,651
  • 4
  • 70
  • 100