0

Quote from: https://skypjack.github.io/2019-02-14-ecs-baf-part-1/

class family { static std::size_t identifier() noexcept { static std::size_t value = 0; return value++; } public: template<typename> static std::size_t type() noexcept { static const std::size_t value = identifier(); return value; } }; 

This is the code required to generate the identifier for a given type when needed:

const auto id = family::type<my_type>(); 

The drawback of this implementation is that it makes use of static local variables and static functions. Therefore, it doesn’t work well across boundaries on some platforms. On the other side it’s straightforward and quite easy to use and to understand.

I have no idea why it doesn't work well on some platfroms?

What was meant by "across boundaries"?

4
  • because of the guarantee of the static keyword. every time you use that, you have to check if you have already set the value, otherwise initialize it Commented May 11, 2020 at 14:55
  • @Berto99: You mean some overhead introduced each time local static is accessed? This is why it doesn't work well on some platfroms? Commented May 11, 2020 at 15:04
  • if you care about the 1% improvement in speed, or you are running in a very low energy / power device (which often happens if you are using C++), than yes, every time you access that variable, you also need to check whether you have already initialized it. Branch prediction will help, but there is always a small overhead Commented May 11, 2020 at 16:09
  • @Berto99 ok, i got your point. Not sure this is what the author meant, but argument is accepted. Commented May 11, 2020 at 18:40

1 Answer 1

1

If you have a static called id and a 3rd party library also has a static called id then they will conflict with each other. Therefore it is better to scope things as tightly as feasible and stay within the bounds of your namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this not answers the question. "on some platfroms" was not taken into account. Statics are local in code example. So local static scope is limited. const auto id = family::type<my_type>(); is just an example of generator usage. Scope of id is unknown in this example and id itself is not a subject of disscution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.