I was working on creating an entity-component system on my own, and in doing this I have gotten help from a lot of YouTube videos, C++ references, recommended books, etc. While I was implementing a functionality that returns IDs for specific components(which are implements as structs), I started wondering:
-
How does this double-static thing work?
-
Would there be a better, simpler way to do this?
For question #1, I am certain that without using static twice, I cannot get the function I want, based upon the experimental code below:
#include <iostream>
#include <bitset>
#include <vector>
struct Physics_c {};
struct Render_c {};
struct Collision_c {};
using componentID = std::size_t;
const inline componentID getID() noexcept {
static componentID lastID = 0;
return lastID++;
}
template <typename T>
const inline componentID gettypeID() noexcept{
std::cout << "function called for " << typeid(T).name() << "n";
componentID typeID = getID();
return typeID;
}
template <typename T>
const inline componentID getCompID() noexcept{
std::cout << "function called for " << typeid(T).name() << "n";
static componentID typeID = getID();
return typeID;
}
int main() {
std::cout << gettypeID<Physics_c>() << "n";
std::cout << gettypeID<Physics_c>() << "n";
std::cout << gettypeID<Physics_c>() << "n";
std::cout << gettypeID<Render_c>() << "n";
std::cout << gettypeID<Render_c>() << "n";
std::cout << gettypeID<Render_c>() << "n";
std::cout << gettypeID<Collision_c>() << "n";
std::cout << gettypeID<Collision_c>() << "n";
std::cout << gettypeID<Collision_c>() << "n";
std::cout << getCompID<Physics_c>() << "n";
std::cout << getCompID<Physics_c>() << "n";
std::cout << getCompID<Physics_c>() << "n";
std::cout << getCompID<Render_c>() << "n";
std::cout << getCompID<Render_c>() << "n";
std::cout << getCompID<Render_c>() << "n";
std::cout << getCompID<Collision_c>() << "n";
std::cout << getCompID<Collision_c>() << "n";
std::cout << getCompID<Collision_c>() << "n";
return 0;
}
I don’t get why the value for the static side of typeID
doesn’t change, even if both gettypeID and getCompID get called for every function call. I thought I knew the basics about the static keyword, but this thing has been killing me…
For question #2, I tried to create a vector that contains each typeID, but I think it isn’t any simpler than the previous code, and it is a waste of memory to make another container just to get a proper typeID. Furthermore, I have to manually input all the possible component types to the component ID vector just for the sake of getting a number for each component. Not to mention the code itself is uglier than before…
#include <typeindex>
#include <typeinfo>
#include <vector>
#include <iostream>
struct Physics_c{};
struct Render_c{};
static std::vector<std::type_index> componentVec {};
template <typename T>
void createCompVec () {
componentVec.push_back(typeid(T));
}
int main() {
createCompVec<Physics_c>();
createCompVec<Render_c>();
for (auto p : componentVec) {
std::cout << p.name() << "n";
}
return 0;
}
Would there be a more efficient way to achieve what I want to do?
Luffy_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.