What features of C++ are not compatible with compilers other than Visual Studio?
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.
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"
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:
pragma once. (and pretty much every pragma) Will be ignored by another compiler, which will result in multiple header inclusions. Can cause trouble.
- __int32 and similar types (microsoft specific)
- Everything that comes from windows.h - DWORD/WORD/HANDLE/TCHAR. Also os-specific API and system calls. This includes WinMain().
- every builtin type, macros and keyword that starts with two underscores (____FUNCTION____, __int32, __declspec, etc).
- Certain versionf of *printf functions - swprintf, vswprintf, etc. Some format (%S) specifications behave differently on different compilers.
- *_s functions (strcpy_s, etc).
Here's a list of nonstandard behaviour in VC++: http://msdn.microsoft.com/en-us/library/x84h5b78%28VS.71%29.aspx
The very clean, but non-portable for each, in statement: Visual c++ "for each" portability
精彩评论