[cfe-dev] Building the Bullet Physics SDK with Clang: Success!

Ryan Gerleve aikavanak at gmail.com
Fri Oct 1 23:01:28 PDT 2010


Mostly just for fun, I decided to try compiling the Bullet Physics SDK
(www.bulletphysics.org) with clang. I am happy to report that aside
from a very small modification to the Bullet code, clang built the entire
SDK (libraries and demos) with no problem!

The small refactor was necessary because the soft body library
tries to use static const variables to zero-initialize certain objects.
The objects happen to be structs, without a user-defined constructor,
derived from another struct with a user-defined default constructor.
The reduced test case is

struct Base {
   Base() {}
};

struct Derived: Base {
};

static const Derived d;

Clang says, "error: default initialization of an object of const
type 'const Derived' requires a user-provided default constructor."
G++ accepts this code.

Looking at the C++03 standard, I'm a bit confused. Section 8.5 paragraph 6
says that "Every object of static storage duration shall be zero-initialized at
program startup before any other initialization takes place." But it also says
in paragraph 9 of the same section that "If no initializer is specified for an
object, ... if the object is of const-qualified type, the underlying class type
shall have a user-declared default constructor."

My intuition tells me that static objects get zero-initialized first,
then default-constructed a bit later, all during program startup. Is this the
correct interpretation of the standard? If so, it sounds like it goes against
the C++ principle of not paying for things you don't use. Why zero-init an
object if it's just going to be default-initialized immediately after?

Of course, if this is the case, then clang is correct, and g++ & Bullet are
not standard-conforming.

-Ryan



More information about the cfe-dev mailing list