erincatto / box2d

Box2D is a 2D physics engine for games
https://box2d.org
MIT License
8.05k stars 1.51k forks source link

Zero initialization of b2Vec2 #683

Closed mwthinker closed 2 years ago

mwthinker commented 3 years ago
// Default initialization
b2Vec2 pos;
// Zero initialization
b2Vec2 pos{};

but I think the expected behavior is Zero initialization for the second version.

struct B2_API b2Vec2
{
      b2Vec2() = default;
// ...
};

By doing this you get no performance hit by default but still give the user a possibility to zero initialization. And no surprise when it is not zero initialized,

franklange commented 3 years ago

I checked the initialization jungle on cppreference.com and since all the math classes: a) are all trivially constructable and b) don't have user-defined default constructors this should be fine.

Meaning: it should just be covered by Explanation, Effects 2), post C++11, without the extra note about compilers applying. https://en.cppreference.com/w/cpp/language/value_initialization

Opened a PR on my fork since it said PRs are generally discourage here on the main repo? https://github.com/franklange/box2d/pull/1

Let me know if this is wanted and I'll point it to master here :)

erincatto commented 2 years ago

Thanks for the report. I changed the default constructors in a1225ee95b1203b463b6d30469606a7a70899f67