13

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)?

Jonathan
  • 752
  • 1
  • 9
  • 19

3 Answers3

17

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
9

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.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    Very good point. Templates in particular can't be guaranteed to work properly if different versions are combined, even from the same compiler. – Mark Ransom Feb 06 '13 at 05:06
  • 3
    Why do libraries like libFLAC ( http://flac.sourceforge.net/api/classFLAC_1_1Decoder_1_1File.html ) export functions that take `std::string` parameters in that case? – us2012 Feb 06 '13 at 05:16
  • @us2012 If a library in it's api uses any of C++ features (overloads, classes, namespaces, etc.) then, in order to use the library, you are *forced* to compile it on your compiler. Either a library is already pre-built by the vendor, or you are provided the source code so you can build it yourself. – Red XIII Feb 06 '13 at 12:32
  • @us2012 - because this concern is a bit overwrought. It's **possible** that binaries compiled with the same compiler and different compiler switches won't interoperate, but compiler vendors are pretty careful to avoid this kind of problem. If this was a major problem, the standard libraries that ship with the compiler also wouldn't be usable. – Pete Becker Feb 06 '13 at 13:32
  • 1
    @Pete the standard libraries that ship *with* the compiler are obviously also *compiled with* the compiler. – Mahmoud Al-Qudsi Feb 06 '13 at 17:29
  • @MahmoudAl-Qudsi - yes, they are. The message asserts that you must have "compiled both in the exact same way at the exact same time." – Pete Becker Feb 06 '13 at 17:30
1

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

Thagi
  • 81
  • 2