title: Within an ECS model, what is the most appropriate observer container?
body: <p>I'm implementing an <a href="https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system" rel="nofollow noreferrer">ECS</a> model in C++ for a game I'm designing. To handle the component-to-component communications, I am exercising an observer system in which components can subscribe to (i.e. observe) other components.</p>
  
  <p>Because I plan on using an object pool, it is necessary that my entities (and thus components) occupy a pre-determined amount of memory (i.e. no heap memory after pool has been constructed). </p>
  
  <p>When implementing the "subject" class within the observer model, it is necessary for the subject to hold references to all of its observers.</p>
  
  <p>Given the fact I'm using an object pool for my components, it is necessary that all the data within them must be statically allocated and not rely on containers that might make memory request calls to the heap (e.g. std::vector).</p>
  
  <p>My current design features a std::array that contains pointers to the observers, however, I feel like this is a rather clunky design. First, I'm unable to change the maximum number of observers that can subscribe to a subject (because I'm must predetermine the array length at compile time). Second, its' clunky to remove an observer from a subject because I must shift every element back to avoid a "gap" in the std::array.</p>
  
  <p>However, I do appreciate the ability to iterate quickly through a std::array when notifying subscribers. Also, due to the nature of the components subscribers, I won't have to "unsubscribe" from a subject too often. Thus the earlier posed difficulty of removing a reference from the array is somewhat moot.</p>
  
  <p>Anyways, I'm curious if there are other STL containers that might be more appropriate for my application. </p>