Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the impact of implementation-dependent language features in C++?

Tags:

c++

The following is an excerpt from Bjarne Stroustrup's book, The C++ Programming Language:

Section 4.6:

Some of the aspects of C++’s fundamental types, such as the size of an int, are implementation- defined (§C.2). I point out these dependencies and often recommend avoiding them or taking steps to minimize their impact. Why should you bother? People who program on a variety of systems or use a variety of compilers care a lot because if they don’t, they are forced to waste time finding and fixing obscure bugs. People who claim they don’t care about portability usually do so because they use only a single system and feel they can afford the attitude that ‘‘the language is what my compiler implements.’’ This is a narrow and shortsighted view. If your program is a success, it is likely to be ported, so someone will have to find and fix problems related to implementation-dependent features. In addition, programs often need to be compiled with other compilers for the same system, and even a future release of your favorite compiler may do some things differently from the current one. It is far easier to know and limit the impact of implementation dependencies when a program is written than to try to untangle the mess afterwards.

It is relatively easy to limit the impact of implementation-dependent language features.

My question is: How to limit the impact of implementation-dependent language features? Please mention implementation-dependent language features then show how to limit their impact.

like image 679
Bill Avatar asked Mar 09 '09 08:03

Bill


People also ask

What does implementation dependent mean?

Implementation dependent means that a certain construct differs from platform to platform but in a defined, well-specified manner. ( e.g. the va_arg family of macros in C varies between posix and windows) Undefined behaviour means that anything (literally) could happen.

WHAT IS &N in C?

&n writes the address of n . The address of a variable points to the value of that variable.

How do you implement language?

There are two general approaches to programming language implementation: Interpretation: The program is read as input by an interpreter, which performs the actions written in the program. Compilation: The program is read by a compiler, which translates it into some other language, such as bytecode or machine code.

Where C language is used nowadays?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


3 Answers

Few ideas:

  • Unfortunately you will have to use macros to avoid some platform specific or compiler specific issues. You can look at the headers of Boost libraries to see that it can quite easily get cumbersome, for example look at the files:

    • boost/config/compiler/gcc.hpp
    • boost/config/compiler/intel.hpp
    • boost/config/platform/linux.hpp
    • and so on
  • The integer types tend to be messy among different platforms, you will have to define your own typedefs or use something like Boost cstdint.hpp

  • If you decide to use any library, then do a check that the library is supported on the given platform

  • Use the libraries with good support and clearly documented platform support (for example Boost)

  • You can abstract yourself from some C++ implementation specific issues by relying heavily on libraries like Qt, which provide an "alternative" in sense of types and algorithms. They also attempt to make the coding in C++ more portable. Does it work? I'm not sure.

  • Not everything can be done with macros. Your build system will have to be able to detect the platform and the presence of certain libraries. Many would suggest autotools for project configuration, I on the other hand recommend CMake (rather nice language, no more M4)

  • endianness and alignment might be an issue if you do some low level meddling (i.e. reinterpret_cast and friends things alike (friends was a bad word in C++ context)).

  • throw in a lot of warning flags for the compiler, for gcc I would recommend at least -Wall -Wextra. But there is much more, see the documentation of the compiler or this question.

  • you have to watch out for everything that is implementation-defined and implementation-dependend. If you want the truth, only the truth, nothing but the truth, then go to ISO standard.

like image 100
Anonymous Avatar answered Sep 23 '22 10:09

Anonymous


Well, the variable sizes one mentioned is a fairly well known issue, with the common workaround of providing typedeffed versions of the basic types that have well defined sizes (normally advertised in the typedef name). This is done use preprocessor macros to give different code-visibility on different platforms. E.g.:

#ifdef __WIN32__
typedef int int32;
typedef char char8;
//etc
#endif
#ifdef __MACOSX__
//different typedefs to produce same results
#endif

Other issues are normally solved in the same way too (i.e. using preprocessor tokens to perform conditional compilation)

like image 26
workmad3 Avatar answered Sep 21 '22 10:09

workmad3


The most obvious implementation dependency is size of integer types. There are many ways to handle this. The most obvious way is to use typedefs to create ints of the various sizes:

 typedef signed   short  int16_t;
 typedef unsigned short  uint16_t;

The trick here is to pick a convention and stick to it. Which convention is the hard part: INT16, int16, int16_t, t_int16, Int16, etc. C99 has the stdint.h file which uses the int16_t style. If your compiler has this file, use it.

Similarly, you should be pedantic about using other standard defines such as size_t, time_t, etc.

The other trick is knowing when not to use these typedef. A loop control variable used to index an array, should just take raw int types so the compile will generate the best code for your processor. for (int32_t i = 0; i < x; ++i) could generate a lot of needless code on a 64-bite processor, just like using int16_t's would on a 32-bit processor.

like image 20
jmucchiello Avatar answered Sep 22 '22 10:09

jmucchiello