views:

70

answers:

4

I was told to avoid using features of C++ like these as it makes it difficult to port the code to other compilers.

The example I was given was using #ifdef instead of #pragma once in my header files.

+3  A: 

Well, this is challenging to answer, because each compiler is different - and, more specifically, #pragma statements are not a feature of C++. #pragma means, by definition "a command to send to the compiler":

"Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler." MSDN

so, essentially, whenever you see #pragma, it means "what follows next is not part of the language standard, and so may be different for every platform you target/compile on"

Matt
+1  A: 

Here's a list of nonstandard behaviour in VC++: http://msdn.microsoft.com/en-us/library/x84h5b78%28VS.71%29.aspx

ho1
+3  A: 

Those are not "C++ features", they are non-standard "extensions", non-standard functions, and "compiler features" provided by compiler developer.

short and incomplete list of microsoft-specific features that will cause trouble during porting:

  1. #pragma once. (and pretty much every pragma) Will be ignored by another compiler, which will result in multiple header inclusions. Can cause trouble.
  2. __int32 and similar types (microsoft specific)
  3. Everything that comes from windows.h - DWORD/WORD/HANDLE/TCHAR. Also os-specific API and system calls. This includes WinMain().
  4. every builtin type, macros and keyword that starts with two underscores (_FUNCTION_, __int32, __declspec, etc).
  5. Certain versionf of *printf functions - swprintf, vswprintf, etc. Some format (%S) specifications behave differently on different compilers.
  6. *_s functions (strcpy_s, etc).
SigTerm
A: 

The very clean, but non-portable for each, in statement: http://stackoverflow.com/questions/197375/visual-c-for-each-portability

jason.rickman