Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Is all of "std" cross platform?

I keep trying different search terms for this question and I'm just finding noise on both Google and stackoverflow. If I write code using C++'s standard library (std), is it all basically guaranteed to compile for Windows, Mac, and Linux (and hopefully work as intended)?

like image 989
Jonathan Avatar asked Feb 06 '13 04:02

Jonathan


3 Answers

The standard defines what it means to be a C++ compiler, and all compilers claiming to be C++ should conform to the standard; any that don't can be considered buggy. All of the major compilers try their best to be conforming.

There are multiple standards to be concerned with here - C++98, C++03, C++11, C++14, C++17, and work has started on C++20. Sometimes the features in the latest current standard won't be implemented in every compiler. If you stick to C++03 you should find wide conformity.

Everything in the std namespace should be part of the standard, by definition.

like image 153
Mark Ransom Avatar answered Oct 21 '22 23:10

Mark Ransom


Code is guaranteed to compatible across all standards-compliant compilers/platforms, but it is important to note that the ABI is not, i.e. you may not assume it safe to link across binaries created from different compilers/versions/platforms.

In practice, this means don't pass STL objects like string or vector around across from one library to another, unless you compiled both in the exact same way at the exact same time. This is especially important when passing pointers to dynamic data: you can't use shared_ptr in your library APIs unless you can meet the said-guarantee, you'll need to use regular pointers instead.

like image 35
Mahmoud Al-Qudsi Avatar answered Oct 21 '22 23:10

Mahmoud Al-Qudsi


The argument is that anything that is not STL compliant is not C++ compliant, and so, in one respect, yes, all STL is cross-platform.

However, be aware that some part of STL are allowed to be implementation defined. For example see type_info::name

like image 43
Thagi Avatar answered Oct 22 '22 00:10

Thagi