C++ used by Box2D

I got into this post while looking for the reason why Box2D is not using namespaces.
It says namespaces are not used because some poor compilers don't support it.

I'm wondering:
  • which C++ features are cut out because of this reason?
  • which C++ compilers (or what versions of what C++ compilers) are supported/taken-into-account by Box2D?

I noticed that things like templates and operator overloading are used: is there really any C++ compiler supporting such stuff and not supporting templates?

Moreover I noticed that a namespace (std) is actually used (thus breaking this convention) twice: in Collision/b2BroadPhase.h at line 188 and in Common/b2Math.h at line 39.



PS/Disclaimer:
Any time I post something in this section I feel like I'm here to insult Erin or his work.
I hope not to be seen like this: I appreciate a lot Box2D and love Erin for it. What I'm trying to do here is to give suggestion to improve Box2D and/or trying to understand its structure. If any feature I suggest gets rejected even without any valid reason that's fine, of course.
I really am sorry if I do result pedantic and annoying.
My opinion is that just because C++ offers feature X, doesn't mean I should use feature X. For example, when you go buy a car, it is not wrong to skip some of the options. It is okay to order a hamburger without ketchup.

Much of this is personal preference.

I use the std::sort algorithm because it is fast and saves me time, not because I like namespaces. I rarely need to debug STL code, but I do need to debug Box2D code. Have you ever tried to debug an STL container? It is a mess on the platforms I've worked on. Also it is fairly trivial to swap out std::sort for something else. It is not trivial to switch containers in many cases.
Agree with you that not all language features are always the best thing. I also try to avoid RTTI and exceptions in my own program, for example.

I was interested in understanding what language features are excluded for the reason that they are not well supported by many compilers, because that's the motivation I found on the link posted in the first message, but I completely disagree with it: I don't think there's any compiler out there not supporting namespaces; namespaces entered the standard when C++ standard was born: 12 years ago, 8 years before Box2D was born!
Such motivation would instead hold for other features, like many non-trivial template usage (like partial specialization, but even simpler template features), C++0x additions (obviously!) etc.

To directly reply to your answer, I got nothing about your decision of avoiding STL: it's just a library (although it's required by the language itself) that, like any other library, offers tools to ease people life. If you don't need such tools, if you don't feel comfortable with them, if you don't think they're adapt to the project (etc.), you're right avoiding it. Besides it mostly concerns Box2D internal representation, more than Box2D public interface, thus it's only about you, not your users. If anything it's even better for your users, since they won't be forced to used STL to any extent. This said, I'd suggest to try to be STL-friendly as much as possible (like by using STL-compatible iterators), unless it would require more work or would have some side effects.
In my first post I cited namespace std just to say "Box2D code is not within a namespace b2 because namespaces are not well supported? But wait, actually Box2D IS using some namespace in its function: namespace std!"

However the thing I was going to ask for this time, were namespaces.
Is it really because namespaces are not so well supported? Unless that's actually the case, I think they wouldn't add any unwanted side effect:
  • they shouldn't slow down your build times
  • shouldn't make code harder to debug (errors will talk about eg. b2::Mul instead of b2Mul; I think it's even easier to read if you're used to it, and makes no difference in the other case)
  • anything else? well, they shouldn't harm in any other way!
and, in my opinion, namespaces should make it a lot easier to work with Box2D: if you're within the b2 namespace (or if you are using namespace b2;) you won't need to use b2Vec2, b2Mul, b2Abs and b2Min, just Vec2, Mul, Abs and Min.

In my opinion a valid motivation to avoid namespaces would be that it would break compatibility with existing code (although a one-line script could refractor it everywhere) breaking a convention that was born with the project, and that for the few days it will make bleed eyes to who's intensively using Box2D (like you).
I agree with Erin, the stl is no panacea, it is difficult to debug.
I've had trouble in the past debugging name spaces where the debugger got confused about variables in different name spaces. The poor debugger prevented me from debugging my code. Not good.

As far as I can tell, the only advantage b2:: has over b2 as a prefix is that b2:: is easier to change in the case of a naming conflict. However, it is still a pain to deal with the conflict.

What do you see as the advantage of name spaces?
My guess is that most users will simply "using namespace b2" anyway, except those who have libraries that may conflict with certain names.

-P
The purpose of namespaces it to avoid conflicts between different symbols.

This issue was addressed in C by prefixing a string to any symbol in the global scope.
This is what Box2D does and it should work: it's unlikely to find an uncorrelated function with the same b2 prefix, and even more unlikely is the need to use it in a project together with Box2D.

However namespace have also got another purpose: they group all the symbols of a project/library/space under a common name.
I find this to be really nice when I code, becasue:

  • When you are writing some code within a namespace you can omit the namespace name. I personally think box2d code would be a lot easier to read if b2Vec2 was called just Vec2.
    • Through the using directive you could also omit the namespace in the scopes where you want to: even the user code could use just Vec2 instead of b2Vec2, in their functions/classes that interact with Box2D (or even in the whole project).

  • Many C++ tools have features that apply to all the symbols in a namespace:
    • Some profilers can tell you how much system time was taken by all functions in a given namespace.
    • Some IDEs suggest you function names when you type a namespace (as soon as I type the second colon in namespace::, QtCreator pops up a window with the list of all the names like in this screenshot.
    • Things like refactoring are of course available of whole namespaces.

    I never got into any tool that lets you do the same things on all the functions sharing a given prefix.

  • You could use a string more meaningful (and univoque) than b2 for a namespace, like box2d. This will make symbol conflicts even less likely, and users will be free to define a namespace alias (ie: using b2 = box2d) if they prefer a shorter name.

These are the reasons why I love namespaces and I'd like to see them everywhere.
No auto-completion, and the need to put b2 as prefix for everything is what makes me mad the most.


The debugger I use (GDB) has got no problems with namespaces.
However if I had to use a debugger unable to resolve namespaces wouldn't know if I'd use them.

Another problem about switching to namespaces is that it would break both ABI and API compatibility with existing code.

There are tricks to solve both problems (but they could possibly spawn other problems) using the C preprocessor:
  • Use macros to open/close namespaces:
    Code:
    #ifdef BOX2D_NAMESPACE // namespace support enabled
      #define BOX2D_NAMESPACE_OPEN namespace box2d {
      #define BOX2D_NAMESPACE_CLOSE }
    #else // no namespace support
      #define BOX2D_NAMESPACE_OPEN // nothing
      #define BOX2D_NAMESPACE_CLOSE // nothing
    #endif

  • Use macros to override any Box2D symbol in the global namespace
    Code:
    #ifdef BOX2D_NAMESPACE_PURE // for clean code
      // Box2D doesn't define global symbols - no b2-prefixed symbols
      // b2Vec2 (etc etc) are not available
    #elif defined(BOX2D_NAMESPACE) // namespace support enabled
      #define b2Vec2 box2d::Vec2
      // etc etc
    #else // no namespace support
      // b2Vec2 (etc etc) are already defined
    #endif

By doing this you wouldn't break ABI compatibility (if namespace support is disabled -- ie: BOX2D_NAMESPACE is not set), nor API compatibility (unless you enable BOX2D_NAMESPACE_PURE).

This solution, however, wouldn't let you use prefix-free names (ie: Vec2 instead of b2Vec2 in Box2D internals, since they wouldn't be defined when namespace support is off.
Addendum to your last point, you could define a clever macro to prepend b2 if you're compiling under the special C mode, something like:

#define CONCAT(x,y) x##y

#ifdef BOX2D_NAMESPACE
#define B2_PREFIX(x) x
#else
#define B2_PREFIX(x) CONCAT(b2,x)
#endif

although macros can increase the ugliness of the code tenfold when used in this manner. Not to mention you'd also have to combine this with your code above to make sure both internal and external code are using the same identifiers.

I do support the namespace idea, it would indeed reduce the verboseness of having b2 prefix the actual identifiers (this is how it is in the C# sourceports; the code looks much simpler in Farseer and Box2CS).

Quote:
No auto-completion, and the need to put b2 as prefix for everything is what makes me mad the most.


I've always been a bit sad that IDEs haven't moved to popping open the intellisense when you start typing in C++ yet. It's done in Visual C#, and it can indeed be done in Visual C++ (VisualAssist enables it), but it's just not included in the regular feature list for some reason. Also, you'd still have to prefix with the namespace name if you're not "using" it, but I guess if you ARE using it, the code would appear much cleaner without a bunch of b2's sticking out everywhere.

-P
C++ is notoriously hard to parse. Other languages like actionscript come with a nice var or function keyword to indicate what might be next in the parse tree.
Well yes, but the fact is that it's already being done; I mentioned VisualAssist, which uses the VC++ intellisense data already existed to pop things up as you type, and it works absolutely fine. The fact is that it's just not being added as a regular feature that I don't understand.

-P

Original thread: https://box2d.org/forum/viewtopic.php?t=6096