Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Portable C++"? [duplicate]

Tags:

c++

Around the Internet I see libraries that claim to be written in portable C++, as if it was a (maybe unofficial) standard.

Is there a precise definition of what is portable C++, and if so, what is it ?

I'm not asking for common practices for writing portable code, but if there really is something we can call "portable c++".

like image 994
renardesque Avatar asked Apr 17 '16 10:04

renardesque


People also ask

What is portable C code?

Portable code is that which compiles with various compilers and runs exactly the same on various architectures without modification. If your code depends on the sizeof(T) of an integral type, in order to keep it portable you should use the types in stdint.h like uint8_t or uint32_t.

What is portable in C++?

`portable' means your source code compiles without maintainance across one or more of. versions of the same compiler. different compilers. different platforms. changes in the language.


2 Answers

Portable C++ is quite ambiguous.

However, if you want to achieve portability, only use the Standard Library of C++, and no platform specific code (like read, write syscalls on Linux), no compiler specific intrinsics (like GCC's C / C++ extensions), or inline assembly for a specific CPU.

Keep in mind, that even this might not be "portable". You can compile C++ for a wide variety of platforms (including embedded), and not all of these platforms might ship with a Standard C++ library, or have a compiler supporting the latest and greatest C++ features (C++11, C++14 comes in mind).

True portability can not be achieved, you can, however, achieve portability for the most common platforms, or commit to support these X platforms, and build a platform specific solution for each of them (which is a considerable amount of work, and results in a lot of #ifdef OS1 code).

like image 166
Leandros Avatar answered Nov 12 '22 16:11

Leandros


Portable C++ code means that such code can be compiled for (almost) any platform and by any implementation. So the goals are, it's unimportant if the program should be running on

  • different operating systems (windows, linux, OSX)
  • different architectures (x86, x86-64, titanium , sparc, arm)
  • different runtime libraries/compilers (gcc, clang, MSVC)

To achieve this, you have to consider many aspects - Don't use implementation defined APIs and behavior, only use the standard library - Don't use architecture defined assumptions and behavior, like that char has 8 bits, or negative integers are 2-complement and overflow or that int is 32-bit long and so on

Problem is, you often has to use stuff, for which there is no standard in C++, like networking interfaces. So libraries often try to work around this problem, by using different specific solutions for the most popular systems, selected by the preprocessor.

So as you see portability is always to see in a context, because absolute portability is not practical achievable. For example C++ code that is portable for any C++11 compliant compiler (but most compilers aren't 100%, see MSVC 12/2013), or portable for C++11 compliant compilers AND POSIX systems (so all unix systems can use it). And so on.

like image 22
Superlokkus Avatar answered Nov 12 '22 15:11

Superlokkus