Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: is std "magically" there? always?

Tags:

c++

c++11

stl

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...

like image 247
daniel451 Avatar asked Mar 20 '17 21:03

daniel451


1 Answers

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.

like image 140
Bathsheba Avatar answered Sep 16 '22 18:09

Bathsheba