Professional Documents
Culture Documents
Why C++?
What It's All About
By Kyle Wilson
Who Is This Guy? Saturday, July 15, 2006
The List A number of different on-line forums (most notably the SWEng-
GameDev mailing list) have seen recent discussion of whether C++ is a
Complete Archive wise language choice for game development. When people have to make
that decision in the real world, the dominance of C++ has been
Personal overwhelming. But if on-line discussion is any indication, there's also a
good deal of dissatisfaction with that choice.
RSS Feed
The attacks on C++ come on two flanks. From the low-level side comes
People the argument that the language supports too high a level of abstraction,
and that we should go back to programming in straight C. This argument
holds that writing efficient code in C is easier than writing efficient code
in C++ because the level of information hiding afforded by C++ hides
inefficiencies from cursory perusal. For example, in C, you can read the
Search
line
a = b + c;
and know that a, b and c are primitive types and that the addition and the
assignment will generate, at most, a few lines of assembly code. In C++,
a, b and c could be objects of any size calling overloaded addition and
assignment operators that might generate still more temporary objects,
allocate memory, call sleep(), reformat your hard drive, etc. The only safe
way to prevent such shenanigans is to disable compiler support for C++
language features entirely.
Remarkably, the two sides don't even disagree. Instead, the sentiment is
commonly expressed that straight C is the way to go for core engine code
while less-performance-critical gameplay code should be written in a
high-level scripting language (for a cogent expression of this opinion, see
this post by Robert Blum in the SWEng-GameDev thread). C++ falls
awkwardly in the middle, a jack of all trades and master of none.
Given all this discontent... Why C++? Why has it been so successful, and
does the language deserve to see that success continue?
First, I'll get the meta-reason out of the way: Nothing succeeds like
success. The fact that C++ is the dominant language for game
development gives it a lot of inertia. If you want to hire experienced
programmers, it's much easier to recruit C++ gurus than it is to find
OCaml talent. If you're going to develop for consoles, Microsoft and Sony
provide C++ compilers, but for any other language you're on your own.
And if you want to link with middleware, most modern game middleware
packages--including Gamebryo, Havok, FMOD, SpeedTree and the
Unreal Engine--are written in C++. Developing in another language, even
C, is a lonely path.
But rather than dwelling on the advantages that derive from its existing
popularity, I'd like to focus on the inherent strengths of C++ for game
development. In doing that, it's important to understand how and why C++
evolved into the language it is today. C++ grew out of C. C was a systems
programming language that co-evolved with the Unix operating system
and that gained popularity as Unix spread. C was tight, close to the metal.
You can look at a C function and tell almost exactly what assembly will
result from compilation.
C++ adds layers of abstraction. C++ began as a set of extensions to C to
add support for the object-oriented concepts found in Simula, classes and
inheritance. As creator Bjarne Stroustrup writes in his C++ FAQ, "Since
1987 or so, the focus of development the C++ language and its associated
programming styles have been the use of templates, static polymorphism,
generic programming, and multiparadigm programming." C++ continues
to evolve to support new design paradigms.
The growth and evolution of C++ has been a mixed blessing. On the one
hand, C++ is truly a language for multi-paradigm development: the
language supports generic, imperative and object-oriented programming.
Any C++ program, or part of a C++ program, can be written in the
appropriate style to best model a particular problem domain. On the other
hand, C++ has been a victim of its own success. As new core language
and standard library features have been added to meet new needs,
programming in C++ has become a powerful but complicated business.
It's easy to hire people who call themselves C++ programmers, but
engineers who understand both the low-level details of the C++ object
model and the compile-time power of C++ template metaprogramming
are hard to come by. And C++, in the hands of an programmer without a
good grasp of the language, is a dangerous thing.
Despite this risk, I think that the answer is better education, not a
wholesale rush back to programming in C. Because in the right hands, C+
+ is a more powerful, safer, and--yes, really--more efficient language than
C is.
Safer? In C, if we write
MAX(x++,2)
While I prefer C++ to C, I do agree that a C++ engine core works best in
combination with gameplay code written in a language that supports a
higher level of abstraction. That's not because C++ lacks programming
constructs of more modern languages, though. I confess that I haven't
worked with aspects or closures enough to even know whether they give
great productivity improvements. I have done enough procedural
programming to doubt that code written in a procedural language is going
to be inherently more clean and elegant than code written in C++. And I'm
generally skeptical of the benefits of garbage collection. Memory is one
resource among many (file handles, critical section locks, D3D resource
reference counts, etc.), and a resource management paradigm that only
covers memory is of limited use. (If you think garbage collection "just
works," read this explanation of the complexities inherent in C#
finalization.)
No, the compelling advantage that a more abstract language has over C++
is faster iteration time. The efficiency and power of C++ make it the best
language choice for core game engine technology, where runtime
efficiency is important and iteration time is not. For gameplay code,
however, those requirements are reversed. The ability to iterate quickly on
gameplay and get immediate feedback on design changes is paramount.
But gameplay logic executes infrequently compared to physics or graphics
routines, so highly-optimized native-code inner loops are less important.
For rapid iteration, you want a simple language that compiles quickly or
doesn't need to be compiled at all. You want to be able to make changes
and see their effect immediately in-game, without having to restart, or
even reload a level. You do not want the daunting features of C++ that
make experienced programmers pause to think and make novice
programmers write buggy code: pointers, a menagerie of different integer
and floating-point types, multiple string representations and manual
iteration over containers.
It's because of these considerations that Lua has become almost as popular
in the game industry as C++--at least among those game companies that
elect to use an off-the-shelf scripting language. Lua is lightweight,
dynamically typed and interpreted. It's garbage collected, but as long as all
your other resources are owned and managed by code and not script,
garbage collection works just fine and saves you the headache of dealing
with memory allocation.
Home