5

As a C++ beginner-level programmer I noticed no matter what IDE/compiler you use, you never need to explicitly include stl (Standard Template Library). Does this mean I can rely on the stl to be "always available"?

I mean if I want to use std::cout for example, I just include the iostream part of the stl:

#include <iostream>

...and do not need to do something like #include <std> in the first place to continue with something like:

std::cout << "Hello world!" << std::endl;

Furthermore: can I rely on consistency for the stl? Does every function/method of the stl always behave the same way? Or are there any changes between C++ releases, operating systems or compilers?

I ask this because other libraries can really be a pain sometimes, when you do not know about certain pitfalls. For example Eigen (for linear algebra stuff) was really hard for me to get it going and I noticed changing behavior between some releases...

daniel451
  • 10,626
  • 19
  • 67
  • 125
  • 1
    That's a lot of questions. – 101010 Mar 20 '17 at 21:34
  • 4
    You don't `#include ` because `std` is just a namespace. And the C++ standards _do_ introduce changes between them but backwards compatibility is a priority. Different compilers have minor differences as well... – Charles Mar 20 '17 at 21:34
  • 3
    std is a namespace name. Everything from the standard headers is defined in that namespace – NathanOliver Mar 20 '17 at 21:35
  • 3
    Don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Mar 20 '17 at 21:37
  • 1
    `std` is the namespace that all the names defined by the C++ standard are required to use. Any standard-conforming implementation will have it. Just write your code according to the standard and you should be fine. – Barmar Mar 20 '17 at 21:37

5 Answers5

9

Yes, notwithstanding special allowances for freestanding implementations which are permitted to provide only a subset, the C++ standard library must ship with every C++ compiler as it is part of the language specification. (What started out as an extension to C++ was originally called the standard template library but now a better term is the C++ standard library.)

The C++ standard library is extremely well specified - you always need to include various header files to bring in standard library components. The headers you need do not change from compiler to compiler. In this respect it is machine independent.

As the standard evolves the philosophy is to minimise compatibility breaks but there have been some: e.g. the redefinition of the auto keyword in C++11, and the deprecation of std::auto_ptr from that standard onwards.

Most components are indeed inside the reserved namespace std - some functions are not for legacy reasons. Finally it's better to use std:: explicitly rather than to bring in the entire std namespace into the global one by using

using namespace std;

else you render your code vulnerable to namespace ambiguities.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks! Thus, to avoid namespace ambiguities it is "good practice" to always use `std::` explicitly? And there's no other downside than some more characters to type? I saw plenty of tutorials doing `using namespace std;` and didn't know why you ever might want to leave this out, since it increases the number of characters you need to type every time.. – daniel451 Mar 20 '17 at 21:50
  • 5
    Tutorials always do this for clarity but it is a disaster in large codebases. – Bathsheba Mar 20 '17 at 21:50
  • 2
    I always have to question the approach of any tutorial that does that: if the tutorial doesn't want to demonstrate good C++ programming practices from the start, what real value does it have? – Daniel Schepler Mar 20 '17 at 22:05
  • 3
    "the C++ standard library must ship with every C++ compiler as it is part of the language specification" -- that's true for "hosted implementations," but the embedded world often uses "freestanding implementations" which don't include the standard libraries. The standard allows for both kinds. Certainly, a beginner is unlikely to encounter a freestanding implementation. – Adrian McCarthy Mar 20 '17 at 22:50
  • 1
    @AdrianMcCarthy: Even "freestanding" implementations are required to ship certain parts of the standard library. Basically, language support stuff (`std::type_info`, `std::initializer_list`, etc), the type traits, and atomics. – Nicol Bolas Mar 21 '17 at 01:00
4

Nearly all C++ compilers will come with the Standard C++ library. These are called "hosted implementations." The library is well defined, and, while it does grow, it rarely changes in ways that would break older code. It's a major goal of the standards committee to maintain backwards compatibility.

The standard also allows for "freestanding implementations," which come with a very stripped-down library that's implemented entirely in the headers. You might encounter this when developing code for an embedded system, especially if that system doesn't have an underlying operating system. These are rare, but they do exist. (Section 7.6.1.13 talks about freestanding implementations.)

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Upvoted - pinched your freestanding bit for my answer. For its length this answer is curiously comprehensive. – Bathsheba Mar 21 '17 at 16:58
3

Does this mean I can rely on the stl to be "always available"?

Yes, the standard library is part of the C++ language. Thus, it is guaranteed that it will "always be available" with any C++ compiler.

Can I rely on consistency for the STL?

Yes, definitely. C++ Standard Library is the distilled effort of brilliant minds that suffered for us in order us not to have every now and then reinvent the wheel or to have to trust third party libraries that in the end might stub you in the back. It is as consisted and as generic as possible.

Does every function/method of the STL always behave the same way? Or are there any changes between C++ releases, operating systems or compilers?

The C++ Standard Library is described in the C++ standard. Thus, it is guaranteed that among C++ standard compliant compilers the C++ Standard Library's functionality attains identical behavior i.e., standard compliant behavior. There are changes between C++ releases (e.g., additional functionality) but backward compatibility is guaranteed. That is, code written in C++98 is still compilable with modern C++ compilers supporting C++17 (with a small number of exceptions).

I ask this because other libraries can really be a pain sometimes, when you do not know about certain pitfalls.

Generally, it is encouraged to use the C++ Standard Library where ever you can and to avoid third party libraries that offer the same functionality as Standard library offers. This is due to many reasons some of them are:

  1. Readability: C++ Standard library is the lingua franca of C++. That is, using the standard library you make your code more readable and you reduce the learning curve for others that are going to maintain your code.
  2. Stability: The standard library is extremely stable, using it you make your codebase more stable.
  3. Guaranteed life time support: Standard library will exist and maintained as long C++ exists.
  4. Interoperability: Standard Library is cross platform.
101010
  • 41,839
  • 11
  • 94
  • 168
0

All C++ compilers ship with some version of the standard template library available to them. Different compilers will support different versions of the standards (i.e. C++98, C++11, C++14, C++17) but you can expect all of them will ship with headers like vector, iostream, etc. That's all there for you! The standardization process is an effort among many compiler vendors to define the functionality in those headers and make them available to the programmer in a standard way, that is, available in every platform there is compliant compiler for.

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48
0

The standard template library is part of the standard. So it should be available on all standard compliant compilers as long as you have an implementation of the standard allocator (more on this).

The standard template library is treated "specially" on the literature for historical reasons, C++ was "born" on 1983 but the first standard was not released until 1998.

The STL first implementation was on 1983 too, but for ADA language. The first port to c++ was not finished until 1992 by HP.

On 1993 it was proposed to be included on the ANSI standard and accepted on 1994 after some modifications.

This makes the STL on the stdlib a fork of the original STL.

On the early days some people used STL only to refer to the original HP library, later maintained by SGI, while other people used STL to refer to the subset of containers on the stdlib.

The STL external behaviour should be "the same" for all implementations, but the memory packaging may vary from one implementation to another.

Today the original C++ STL is no longer maintained, and talking about the STL should refer to the stdlib containers, although the standard itself doesn't use the STL acronym.

Finally, the stdlib containers use the standard allocator for it's memory management. On Linux GCC it's provided by libstdc++, on embedded systems you need to provide an implementation for the standard allocator yourself as you don't have an OS managing the memory for you.

This also applies for other functions that rely on system calls. std::cout won't be available on a bare metal system unless you define an implementation for it.

xvan
  • 4,554
  • 1
  • 22
  • 37
  • Unfortunately, that information is a bit out of date: The current standards indeed include the standard library (which has changed name as well). See Bathshebas answer for details. However, you still have a point about the bare-metal thing: Standard conformant or not, we still need compilers to compile without the standard C++ library. – cmaster - reinstate monica Mar 20 '17 at 22:51
  • Could you point what's out of date? The C++ standard has defined the standard library since the first version, including the STL. – xvan Mar 20 '17 at 23:03
  • I correct myself, the C++ standard library included a fork of the STL. – xvan Mar 20 '17 at 23:10
  • and so, it should be referred to as the standard library, not the STL. – underscore_d Mar 21 '17 at 00:31
  • Well, if you would edit that into your answer, I would be able to remove my downvote... – cmaster - reinstate monica Mar 21 '17 at 07:57