views:

2076

answers:

12

Unquestionably, I would choose to use the STL for most C++ programming projects. The question was presented to me recently however, "Are there any cases where you wouldn't use the STL?"...

The more I thought about it, the more I realized that perhaps there SHOULD be cases where I choose not to use the STL... For example, a really large, long term project whose codebase is expected to last years... Perhaps a custom container solution that precisely fits the projects needs is worth the initial overhead? What do you think, are there any cases where you would choose NOT to STL?

+11  A: 

Usually, I find that the best bet is to use the STL with custom allocators instead of replacing STL containers with hand rolled ones. The nice thing about the STL is you pay only for what you use.

Evan Teran
+5  A: 

I think it's a typical build vs buy scenario. However, I think that in this case I would almost always 'buy', and use STL - or a better solution (something from Boost perhaps), before rolling my own. You should be focusing most of your effort on what your application does, not the building blocks it uses.

Trent
+30  A: 

The main reasons not to use STL are that:

  1. Your C++ implementation is old and has horrible template support.
  2. You can't use dynamic memory allocation.

Both are very uncommon requirements in practice.

For a longterm project rolling your own containers that overlap in functionality with the STL is just going to increase maintenance and development costs.

Greg Rogers
This is what it comes down to.
Christopher
While I agree, I would also throw in concurrency as a reason not to use the containers. However, I would use them for any non-shared resource.
Matt Price
Greg - (1) is probably very uncommon, but (2) is highly situational. In certain development environments (e.g. embedded platforms and realtime systems), dynamic allocation is either forbidden or heavily discouraged. They may not be big environments, but within the environment it's universal.
Tom
@Tom: I always thought the average embedded program allocated all of its memory at the start. Why shouldn't you use STL containers for that?
nikie
Not wanting exceptions might be another reason not to use STL
sbk
You can also use the STL in cases where there is no dynamic memory allocation by just using your custom allocator.
Albert
+5  A: 

I don't really think so. In making my own containers, I would even try to make those compatible with the STL because the power of the generic algorithms is too great to give up. The STL should at least be nominally used, even if all you do is write your own container and specialize every algorithm for it. That way, every sorting algorithm can be invoked sort(c.begin(), c.end()). If you specialize sort to have the same effect, even if it works differently.

coppro
+1  A: 

I have found problems in using STL in multi-threaded code. Even if you do not share STL objects across threads, many implementations use non-thread safe constructs (like ++ for reference counting instead of an interlocked increment style, or having non-thread-safe allocators).

In each of these cases, I still opted to use STL and fix the problems (there are enough hooks to get what you want).

Even if you opt to make your own collections, it would be a good idea to follow STL style for iterators so that you can use algorithms and other STL functions that operate only on iterators.

Lou Franco
I don't understand. If your STL objects are not shared across threads, why would it matter whether or not an interlocked increment was used?
Ferruccio
Are you maybe using VC++ 6.0? That version has a broken STL implementation when it comes to thread safety.
Nemanja Trifunovic
Yes -- but that wasn't the only one. On Solaris and HP -- I ran into similar issues.
Lou Franco
Ferruccio -- create a string, then copy construct it and send it to another thread. They share a reference count. If I destruct the first string while the next thread is copy constructing it again, the reference count can be corrupted.
Lou Franco
So you *are* sharing STL objects across threads...
Steve Jessop
NO - read again. One object in one thread, a COPY in another thread. Two objects, two threads. Still a bug. The STL is part of standard C++, which has no threads. A threadsafe STL is a common vendor extension; with C++0x we'll expect no less.
MSalters
If you send a reference to an object from one thread (the one it was created in by copy) to another (where the copy is copied again), then you're sharing it between threads. In this case, doing so has messed up the copy-on-write behaviour, even though no one string object was concurrently accessed.
Steve Jessop
Btw, I'm not saying that STL is guaranteed safe in multi-threaded code provided you don't share STL objects between threads. I'm just saying that the described case was not an example of avoiding sharing STL objects, since it created an object in one thread and then accessed it from another.
Steve Jessop
The problem is the sharing is happening completely behind the string abstraction -- I can cause problems with strictly const strings and read-only operations -- because, internally, the strings share a reference count, just copy constructing (passing and returning strings) will cause the problem.
Lou Franco
+21  A: 

There are just so many advantages to using the stl. For a long term project the benefits outweigh the costs.

  1. New programmers being able to understand the containers from day one giving them more time to learn the other code in the project. (assuming they already know STL like any competent C++ programmer would)
  2. Fixing bugs in containers sucks and wastes time that could be spent enhancing the business logic.
  3. Most likely you're not going to write them as well as the STL is implemented anyways.

That being said, the STL containers don't deal with concurrency at all. So in an environment where you need concurrency I would use other containers like the Intel TBB concurrent containers. These are far more advanced using fine grained locking such that different threads can be modifying the container concurrently and you don't have to serialize access to the container.

Matt Price
+1  A: 

Most of the projects I have worked on had a codebase way older than any really usable version of STL - therefore we chose not to introduce it now.

Nemanja Trifunovic
+15  A: 

Projects with strict memory requirements such as for embedded systems may not be suited for the STL, as it can be difficult to control and manage what's taken from and returned to the heap. As Evan mentioned, writing proper allocators can help with this, but if you're counting every byte used or concerned with memory fragmentation, it may be wiser to hand-roll a solution that's tailored for your specific problem, as the STL has been optimized for the most general usage.

You may also choose not to use STL for a particular case because more applicable containers exist that are not in the current standard, such as boost::array or boost::unordered_map.

I always found that the SDL containers are all pretty lightweight. Check out your STL implementation to see how much memory they actually use.
Albert
+1  A: 

The main issue I've seen is having to integrate with legacy code that relies on non-throwing operator new.

Jamie Eisenhart
A: 

I started programming C back in about 1984 or so and have never used the STL. Over the years I have rolled my own function librarys and they have evolved and grown when the STL was not stable yet and or lacked cross platform support. My common library has grown to include code by others ( mostly things like libjpeg, libpng, ffmpeg, mysql ) and a few others and I would rather keep the amount of external code in it to a minimum. I'm sure now the STL is great but frankly I'm happy with the items in my toolbox and see no need at this point to load it up with more tools. But I certainly see the great leaps and bounds that new programmers can make by using the STL without having to code all that from scratch.

KPexEA
Just one question: Are you coding in C, or in C++ ?
paercebal
STL is not external code. It is a standard part provided by all standards-compliant C++ compilers. Given that your toolbox is apparently more than ten years out of date, it may be worth your time taking a look around and seeing what other advances have been made. Okay, I'm joking here, and I know EA has or had a justified aversion to STL. But you are mistaken in describing STL as "external".
ChrisInEdmonton
+2  A: 

One situation where this might occur is when you are already using an external library that already provides the abilities you need from the STL. For instance, my company develops an application in space-limited areas, and already uses Qt for the windowing toolkit. Since Qt provides STL-like container classes, we use those instead of adding the STL to our project.

Caleb Huitt - cjhuitt
Given that STL is included as a standard part of C++, however, it's not so much that you don't add STL to your project but rather, you chose not to take advantage of something that is already there. Which is fine.
ChrisInEdmonton
+2  A: 

Coding for Symbian.

STLPort does support Symbian 9, so the case against using STL is weaker than it used to be ("it's not available" is a pretty convincing case), but STL is still alien to all the Symbian libraries, so may be more trouble than just doing things the Symbian way.

Of course it might be argued on these grounds that coding for Symbian is not "a C++ programming project".

Steve Jessop
The Symbian API leads me to believe they understand neither C++ nor mobile software development.
MSalters
I won't particularly defend it, other than to say that it's 10 years or more old, and seems to have been designed for devices far more constrained than those it's actually used on. Even Nokia used S40 on their low-end devices, raising the question of why SymbianOS made the assumptions it did.
Steve Jessop
For example, on a 32MB RAM smartphone, as a programmer I'm not keen to do extra work in order to avoid a 4 bytes per string memory overhead. I guess that might have made sense at the time, though. And they treated C++ mostly as C with classes, which is unpopular these days.
Steve Jessop