What are the things that I should keep in mind to write portable code? Since I\'m a c++ beginner, I want to practice it since beginning.
Thanks.
OS-independent code is surprisingly hard to do in C++. Consider this trivial example:
#include
int main(int argc, char** argv) {
std::cout << argv[0] << std::endl;
}
That is perfectly valid C++, still it's nonportable because it won't accept Unicode command line arguments on Windows. The correct version for Windows would be:
#include
int wmain(int argc, wchar_t** argv) {
std::wcout << argv[0] << std::endl;
}
Of course that is again nonportable, working only on Windows and being nonstandard. So in fact you cannot even write a portable main() function in C++ without resorting to conditional compilation.