Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Alternative to STL and Boost?

Tags:

c++

stl

boost

C++ is a multi-paradigm language and STL and Boost are built towards the functional paradigm of the language. STL is composed of containers (to hold data), iterators (to access data) and algorithms (functions to manipulate data). Algorithm functions are applied on containers by using iterators. As a side-effect, these methods are not part of the container classes, but are completely separate. (This avoids redundancy for the library writers, but is painful for library users.)

Are there C++ alternatives to STL/Boost which offer such containers in a more traditional object-oriented flavour? I am looking for strings, vectors, linked lists, map, trees, hash tables and such. Containers should be easy to inherit and extend. In comparison, extending classes from STL/Boost is a very bad idea and this is by design of their designers.

PS: Please do not use the reply space below to pontificate the advantages of STL/Boost. I am well aware of them! :-)

like image 838
Ashwin Nanjappa Avatar asked Apr 27 '11 04:04

Ashwin Nanjappa


People also ask

Is boost better than STL?

Ultimately, you should learn both. But STL can be learned in isolation, whereas boost won't make much sense until you understand the STL, since that's what Boost is modeled on, and designed to extend. So learn to use the STL as part of learning c++.

Does boost use STL?

Introduction. Boost. Container library implements several well-known containers, including STL containers.

Is C++ Boost free?

Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications.


4 Answers

Many (most!) older libraries for C++ used containers that bore a much closer resemblance to those used in such things as Java and C#.

A few example of such libraries include COOL, ET++, the NIH Class Library, and Rogue Wave Tools.h++.

Two points:

  1. At most, these are a source if inspiration. I'm pretty sure it's been at least 10 years (and often more like 20) since any of them has been updated. There's virtually no chance that any of them will even compile with any reasonably current compiler.
  2. I want to go on record as pointing out that I'm providing links to these only in answer to a very specific question. I most assuredly do not recommend that you use any of the above code, nor do I recommend that you even use them as inspiration.

To be sure I'm clear here, at least IMO:

  • The allegations in your question are utterly false.
  • What you're trying to do is completely insane!
  • You're wasting your time.
  • Writing code this way is a really, really bad idea. Just say no!
  • If you insist on doing this, you will become a pariah.
    1. Even non-programmers who don't quite understand why, will begin to hate you intensely.
    2. Your dog will use your shoes and bed as his toilet.

You're on your own. You have been warned!

Closed captioning for the humor impaired: of course some of that is meant to be humorous -- it is a really, really lousy idea though

like image 96
Jerry Coffin Avatar answered Oct 15 '22 21:10

Jerry Coffin


This avoids redundancy for the library writers, but is painful for library users.

I don't agree with this premise at all. And even if I did, it's a huge over-generalization that doesn't apply to every library user. But this is a subjective statement anyway, so I'll ignore it.


Are there C++ alternatives to STL/Boost which offer such containers in a more traditional object-oriented flavour?

...

Containers should have methods that allow one to manipulate on them directly. (For example, calling vector.sort() instead of sort(vector.begin(),vector.end()).

Sure. Just make your own containers that have the standard containers as data members and delegate calls to them and to algorithms as needed via member functions. It's rather trivial to implement:

template<typename T>
class MyVector
{
public:
    void sort()
    {
        std::sort(vec.begin(), vec.end());
    }

    // ...
private:
    std::vector<T> vec;
};

There's nothing in C++ that prevents you from doing something like this, ironically thanks to the multi-paradigm nature of C++ that you seem to not agree with.

You can probably use private inheritance and using declarations if you much rather not write out wrapper functions.


STL/Boost make it a pain to derive from their containers and extend them.

That's because you're not supposed to derive from them. The proper way is to use composition, like the code snippet I presented above.

like image 36
In silico Avatar answered Oct 15 '22 21:10

In silico


You're heading the wrong way. If you want to program in Java then program in Java. If you program in C++ then program as C++ programmers do. Always swim with the current, never against it.

like image 27
wilhelmtell Avatar answered Oct 15 '22 22:10

wilhelmtell


STL and Boost are as object-oriented as you can get.

  1. For all theoretical purposes, member function and a free function overloaded on the first argument are the same thing. They behave very similarly in practice, including for inheritance, so in C++ you should really consider free functions taking (possibly const) reference as first arguments to be methods of their first argument.

    Advantage of free functions is they can be defined for existing class allowing you to add an interface to existing class. Which is why STL and especially boost uses them so much. Main advantage of member functions is they can be virtual (but virtual methods should be private anyway!)

  2. You don't want to extend collections by derivation. Generally, you don't want to extend anything by derivation unless it is an abstract base class specifically designed for it. See this question about advantages of composition over inheritance.

like image 31
Jan Hudec Avatar answered Oct 15 '22 21:10

Jan Hudec