Thursday, March 17, 2011

C vs C++

Everyone I know always asks me why I bother to use C++ for game engine development.  Their opnions towards C++ are biased and are backed up by links to irrelevance.  Then there are those who ask why I bother to use the Standard Template Library, for which I reply because it's part of C++.

Most of the people I know who work on game engines or games perfer to use C++ minus the STL in favor of using the C library.  Their agruments always evolve around "it's much faster and simpler to debug" senero, which is acceptable given their knowledge of the STL is little to nothing.

It's hard to profile C vs C++ code because the experiment is hard to control, most people compare strings to char pointers and vectors to arrays, yet they have little knowledge of how the STL actually works.

While it's true a std::vector<> compiles down to native array instructions, people actually never believe it.  Everyone I've talked to brings up the argument that they can never view the contents of a std::vector in a debugger so it cannot compile down to native array instructions; this is because you compile debug builds and in the situation of a debug build the std::vector<> is never optimized to native array instructions. Don't belive me take a look for yourself:

Then there are those who complain that a std::string is slower then a char * yet they never seem to compare the most common use of strings because if they did they would be surprised to know that assignment of a string is exactly the same as assignment of the char pointer in speed and in syntax.
They would also be surprised to know that comparision in actuality is exactly the same. It also seems more syntatically correct too. Lets compare shall we:

if(a==b)            // std::string way
if(!strcmp(a,b)) // char * way

Most of their arguments also float around the idea that C++ is just too complicated and the syntax is horrible. Yet in actuality the reason they bring up that argument is because of their inability to learn how templates or class inheritence works, and instead they resort to doing everything the wrong way. If you want to write C then write C in a .c file not a .cpp file, or atleast have the decency to wrap the section of code in a extern "C" scope. If you can't isolate the C-only code because it's scattered all over the place then think about wrighting your project in C!

No comments:

Post a Comment