

Ask HN: "Restrained C++"? - fogus

In an interview at http://bethblog.com/index.php/2010/10/29/john-carmack-discusses-rage-on-iphoneipadipod-touch/ , John Carmack stated the following:<p><pre><code>    I would prefer to do new game 
    development in (restrained) C++
</code></pre>
My question -- what is "restrained C++"?  I realize that there is a lot of talk about "subsets of C++" and "20% of C++, yadda yadda", but I've not found a satisfactory explanation of what that means in practical terms.<p>Has Carmack himself defined this term?  Does it have a common meaning that those in the C++-know understand?<p>Thanks for taking the time to explore with me.
======
paol
In short, no. It means using the subset of C++ that the author considers good.
Obviously that varies a lot. At one extreme you have what is usually referred
to as "C with classes": swearing off the use of templates, STL, etc.

Having said that, there is some stuff that is almost universally rejected.
IIRC Stroustrup himself said the way checked exceptions were introduced to the
language was a mistake, for example.

------
joeld42
As a game developer, the recipe I like is C++ with classes but I try not to
use inheritance/virtual functions unless there is a clear benefit. Instead,
common code can be grouped into utility classes. For example, the classic
"Shape" base class example with "Circle" and "Triangle",etc derived classes
could be instead a "Shape" class that held a small list of arcs segments and
line segments. Then a "makeCircle", "makeTriangle", "makeRoundRect" function.

If you have a few (hundreds) of objects and many types (circles,
triangles,etc) then the "classic" example is better. But in the real world,
you often have many (millions) of objects and a small fixed number (say, <10)
of different types. Knowing that everything is a "Shape" (and not just
something that implements its interface) is very powerful, for example you
could collapse all your segments into a spatial data structure and not even
keep the "shapes" list around anymore.

One of the best things you can do is realize just because you have classes,
doesn't mean you can't still use plain functions anymore. I hate it when I see
classes like "ObjectMunger" with a function "munge()" ... just write the munge
function unless there's a good reason to do otherwise.

Templates are actually a good feature if you don't get carried away. But too
often I see people use templates like FooMuncher<float> fFooMunch; where
<float> (or something) is the only type that is ever instantiated. Why bother
templatizing something if you're not going to use that flexibility. If you
want a double-precision foo in the future, you can always search and replace
;).

Look at the EASTL library for a nice alternative to the STL that fixes some of
it's warts such as the clumsy allocators.

------
humbledrone
There's no widely accepted definition for "restrained" C++. Not only does it
vary by author, but also by project. For example, template-heavy code might be
restrained on an embedded project (due to code size concerns), but not on a
desktop app.

If you'd like to see another concrete example of a "restrained" C++, check out
the Joint Strike Fighter Air Vehicle C++ Coding Standards. It contains
extremely specific details on what parts of C++ to use. For example, it
disallows the use of unions and exceptions, and restricts heap allocation to
initialization-time only:

<http://www2.research.att.com/~bs/JSF-AV-rules.pdf>

------
mbowcock
I don't know what John Carmacks definition of 'restrained c++' is but this
might interest you - <http://www.cs.washington.edu/homes/tom/c++example/>

"...a 25 page document outlining the most important aspects of C++ and
steering you around those aspects of the language that would need to read an
800 page manual to understand."

Edit: Found this from John Carmack. Mostly c++ convention stuff.

ftp://ftp.idsoftware.com/idstuff/doom3/source/CodeStyleConventions.doc

