12

I'd like to know what the specific differences are between the STL as released by SGI and the ISO C++ standard library. Prompted by this question and not at all answered by this question.

Some differences are obvious, such as the slist and hash_set classes that never made it into the standard. I'm also looking for more subtle differences, such as return value/parameter differences on methods, or different complexity requirements, or different iterator invalidation conditions.

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    possible duplicate of [What is the difference between the standard library and the standard template library?](http://stackoverflow.com/questions/4064010/what-is-the-difference-between-the-standard-library-and-the-standard-template-lib) – Fred Nurk May 06 '11 at 08:46
  • 1
    @Fred: __No, that's wrong! Don't close/merge this!__ – sbi May 06 '11 at 08:51
  • This question had a somewhat misleading title. (I fixed that now.) It's intention is to ask for the ___specific differences___ between the ___original STL___ and those parts of it that got ___incorporated into the standard library___. – sbi May 06 '11 at 08:52
  • 1
    @sbi: Not only do they ask the same thing, as far as I can see, but all of the answers here look appropriate for the other question. – Fred Nurk May 06 '11 at 08:52
  • @Fred: You might want to consider getting glasses. `:)` No offense meant, but I can't see much resemblance between these and those answers except for the term "STL". (See also Mark's comment to my answer here. I, too, had read it wrong originally.) – sbi May 06 '11 at 08:54
  • 1
    The differences are so pathetically small that I can't see a justification for this being a separate question. Any thorough answer to either question will _naturally_ cover both. – Lightness Races in Orbit May 06 '11 at 09:04
  • @Fred, I acknowledged that "possible duplicate" question in my original post. I tried to word this one in a way that would get better responses. – Mark Ransom May 06 '11 at 13:20

7 Answers7

11

SGI STL stuff "missing" in the C++ standard includes

... and I bet you can find a few more.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 2
    it's noteworthy that some of this is in tr1 and will be in c++0x :-). – Evan Teran Mar 11 '11 at 00:40
  • `hash_anything` are not in any C++ standard, but were widely provided by compiler vendors. That's the reason hash tables don't go by that name in C++0x – sbk Mar 11 '11 at 10:35
  • @sbk: A version of them (`unordered_...`) will be part of the next C++ standard, thought. – sbi Mar 11 '11 at 14:47
7

In addition to what larsmans already wrote:

  • std::basic_string got equipped with an STL container interface.

  • Some template features were added to the language to better support the STL, which the STL portion of the standard library can exploit, but weren't available for the orginal STL.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
3

operator[] in std::map has the difference that in the standard it is defined as returning (*((insert(make_pair(x, T()))).first)).second, whereas in the STL m[k] is defined as equivalent to (*((m.insert(value_type(k, data_type()))).first)).second.

The difference is that conforming C++ implementations call make_pair whereas the STL constructs the pair directly. I can think of two differences this makes:

1) The standard permits an additional copy of the pair (and hence of the key and data objects), if RVO fails to kick in for the call to make_pair. As I read it, the STL does not permit this copy (although there is of course a further copy down the line, in insert). This matters if either the key or the value type have copy constructors with observable side-effects.

2) Users can specialize either std::make_pair or std::pair. If they specialize make_pair then their code is guaranteed to be called in standard C++, and guaranteed not to be called in the STL.

Such specializations cause UB if they don't satisfy the requirements of the template, though, and in the case of make_pair I think that if it has any observable effects other than those of creating the pair, then it doesn't satisfy the requirements. So it might be difficult or impossible in this case to write a specialization that's guaranteed to let you work out whether it has been called or not. In practice if the implementation has done the obvious thing and used the code from the standard, then you will easily see a difference...

Users can also ADL-overload make_pair, same caveat, with the extra complication that I'm never quite sure whether mentions in the standard of unqualified function calls require that the implementation make the same unqualified call. I'm sure I've heard that some implementations have made fully-qualified calls to std::whatever in such cases, perhaps erroneously.

Is this the sort of thing you're after?

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • Yes, this would be an example of what I'm looking for. Very much an extreme example, since you're unlikely to see the difference in actual code. – Mark Ransom May 13 '11 at 21:04
2

In the STL, the four-argument version of std::list::splice is guaranteed to have constant time complexity, even when a portion of one list is being spliced into a different list. As a consequence, std::list::size is not guaranteed to have constant complexity, indeed is in effect guaranteed to be Omega(n) in at least some cases. I haven't checked whether the SGI implementation makes it Omega(n) in all cases.

In the C++03 standard, 4-arg splice is only guaranteed to have constant complexity when a section of a list is being moved to a different place in the same list. This means that the size of the list doesn't change, and hence allows for implementations in which size() is constant time. GNU stuck with the STL approach, but I believe Dinkum opted for constant-time size().

In the C++11 standard, size() is required to have constant time, and hence in effect splice is guaranteed to be Omega(n) in certain cases. The STL approach no longer conforms, and as a consequence there is a binary incompatibility in gcc when they added a size field to std::list.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

The STL allows for the possibility that the C++ implementation doesn't support member function templates (in which case, all the member function templates and constructor templates are unavailable). Obviously the standard doesn't permit that.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

STL also had some functional stuff that was sadly missed in stdlib, although C++0x fixes that.

To quote an example for composition fuctors from STL doc

Computes sin(x)/(x + DBL_MIN) for each element of a range.

transform(first, last, first,
          compose2(divides<double>(),
                   ptr_fun(sin),
                   bind2nd(plus<double>(), DBL_MIN)));

Or, for pair selectors:

transform(M.begin(), M.end(), ostream_iterator<double>(cout, " "),
          select2nd<map<int, double>::value_type>());
Cubbi
  • 46,567
  • 13
  • 103
  • 169
0

I'm not familiar with an exhaustive list. However, it is possible to get the original STL and compare it to the Standard, depending on how much time you have and how much detail you want.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • My hope in asking the question was that someone had already done this and was willing to share. So far there has been very little that wouldn't jump out at you as obvious on a quick read. – Mark Ransom May 12 '11 at 23:40
  • That was my hunch (you were hoping for an exhaustive list that somebody had already written, i.e., "more subtle differences, such as return value/parameter differences on methods, or different complexity requirements, or different iterator invalidation conditions"), and larsmans' answer is the longest list I've seen, but it's not exhaustive. – Max Lybbert May 13 '11 at 20:54
  • 1
    I wasn't really expecting one exhaustive answer, but rather bits and pieces from different contributors that would be comprehensive when taken together. – Mark Ransom May 13 '11 at 20:59