To what extent are modern C++ features like:
- polymorphism,
- STL,
- exception safety/handling,
- templates with policy-based class design,
- smart pointers
- new/delete, placement new/delete
used in game studios? I would be interested to know the names of libraries and the C++ features they use. For example, Orge3D uses all modern C++ features including exceptions and smart pointers. In other words, if I would be looking for an example for a game library using modern C++, I would go to Orge3D. But I don’t know if these features deter game studios from using Orge3D.
Further I don’t know if there are other examples. For example, I used Box2D some time before briefly, but it uses only placement new, and class keyword as the C++ features. Even encapsulation is broken in these classes as all members are public.
Ideally, if C++ features would be the best match for all situations, these would be used most often. But it seems not. What are the impedances? The obvious one is having to read stacks of books, but that’s half a reason only. This question is a follow up on “C++ for Game Programming – Love or Distrust?” (From the responses there I got an impression that many C++ features are still not used in games; which is not necessarily the way it should be).
I’ve worked in 2 game companies, seen a number of codebases, and observed a number of debates on matters such as these among game developers, so I might be able to offer some observations. The short answer for each point is that it varies highly from studio to studio or even from team to team within the same studio. Long answers enumerated below:
Used by some studios but not others. Many teams still prefer to avoid it. Virtual function calls do come at a cost and that cost is noticeable at times even on modern consoles. Of those that do use polymorphism, my cynical assumption is that only a small percentage use it well.
Split down the middle, for many of the same reasons as polymorphism. STL is easy to use incorrectly, so many studios choose to avoid it on these grounds. Of those that use it heavily, many people pair it with custom allocators. EA has created EASTL, which addresses many of the issue STL causes in game development.
Very few studios use exception handling. One of the first recommendations for modern consoles is to turn off both RTTI and exceptions. PC games probably use exceptions to much greater effect, but among console studios exceptions are very frequently shunned. They increase code size, which can be at a premium, and literally are not supported on some relevant platforms.
Policy based design… I haven’t come across any of it. Templates are used pretty frequently for things like introspection/reflection and code reuse. Policy based design, when I read Alexandrescu’s book, seemed like a flawed promise to me. I doubt it’s used very heavily in the game industry, but it will vary highly from studio to studio.
Smart pointers are embraced by many of the studios that also use polymorphism and STL. Memory management in console games is extremely important, so a lot of people dislike reference counting smart pointers because they’re not explicit about when they free… but these are certainly not the only kind of smart pointer. The smart pointer idea in general is still gaining traction. I think it will be much more commonplace in 2-3 years.
These are used frequently. They are often overridden to use custom allocators underneath, so that memory can be tracked and leaks can be found with ease.
I think I agree with your conclusion. C++ isn’t used in game studios to as much of an extent as it could be. There are good and bad reasons for this. The good ones are because of performance implications or memory concerns, the bad ones are because people are stuck in a rut. In a lot of ways it makes sense to do things the way they’ve always been done, and if that means C with limited C++ then it means C with limited C++. But there are a number of anti-C++ biases floating around… some justified and some not.