I am developing an entity-based system, and I am trying to assign a specific index to component types:
static std::size_t getNextTypeId() { static std::size_t lastTypeIdBitIdx{0}; ++lastTypeIdBitIdx;
In my game code, I have about 20 types of components declared as follows:
struct CPhysics : public sses::Component { ... }; struct CHealth : public sses::Component { ... }; struct CWeapon : public sses::Component { ... };
In my system object code, I use TypeIdStorage<T>::bitIdx with T one of the component types several times - I expect this to happen:
- If
TypeIdStorage<T> exists, just return TypeIdStorage<T>::bitIdx . - If it does not exist, create it and initialize
bitIdx with getNextTypeId() .
This is what prints when the application starts:
1 2 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
How is it possible that a call to getNextTypeId() returns the same number? This type of conclusion should be impossible.
Does it that the static variable will increase without repetition? I am really confused here.
Tested with g ++ 4.8.1 and clang ++ 3.4 , both in debug mode and in release mode. The same way out.
valgrind doesn't print anything interesting.
clang ++ AddressSanitizer does not print anything interesting.
Setting the entry point of my program to int main() { return 0; } int main() { return 0; } gives exactly the same result. The problem at compile time - but how is this possible? It seems to me that the situation is impossible .
c ++ static static-methods c ++ 11 static-members
Vittorio romeo
source share