What is Portable C ++?

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

Is there an exact definition of portable C ++, and if so, what is it?

I am not asking for general rules for writing portable code, but if there is something we can call "portable C ++".

+7
c ++
source share
2 answers

Portable C ++ is rather ambiguous.

However, if you want to achieve portability, use only the standard C ++ library, not platform-specific code (e.g. read , write syscalls on Linux), no built-in compiler functions (e.g. GCC C / C ++ extension) or built-in assembly for a specific processor.

Keep in mind that even this may not be "portable." You can compile C ++ for a wide variety of platforms (including built-in), and not all of these platforms can come with a standard C ++ library or have a compiler that supports the latest and greatest C ++ features (C ++ 11, C + + 14 means).

True portability cannot be achieved, however, you can provide portability for the most common platforms or take on the support of these X-platforms and build a solution for each platform for a specific platform (which represents a significant amount of work and leads to a lot of code #ifdef OS1 ).

+8
source share

Portable C ++ code means that such code can be compiled for (almost) any platform and any implementation. Thus, goals are not important if the program should work on

  • various 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 take into account many aspects - Do not use the API and behavior defined during implementation, use only the standard library - Do not use architectures, certain assumptions and behavior, for example, that char has 8 bits, or negative integers are 2-complement and overflow, or that int is 32 bits long, etc.

The problem is that you often have to use material for which there is no standard in C ++, for example, network interfaces. Therefore, libraries often try to work around this problem by using various specific solutions for the most popular systems selected by the preprocessor.

So, as you see, portability must always be seen in context, because absolute mobility is not practicable. For example, C ++ code portable for any compiler compatible with C ++ 11 (but most compilers are not 100%, see MSVC 12/2013) or portable for compilers compatible with C ++ 11 and POSIX (so that’s all Unix systems can use this). And so on.

+4
source share

All Articles