title: Templates and runtime component loading in a component-based game engine
body: <p>Suppose I have an component based game engine, and in that engine I have an
  object that stores a repository of properties in a form of a stl map and can
  be accessed through the object access() method, which is a templated call so
  you can access and store any type of data into the repository, eg:</p>
  
  <pre>class component {
      private:
          object * parent;
  
      public:
          component() : parent(0) { }
          void setparent(object * p) { parent = p; }
          virtual void tick() = 0;
  }
  
  class object {
      private:
          std::list compipeline;
  
      public:
          template&lt;typename prop_t&gt;
              access(std::string propname) {
                  static std::map&lt;std::string, prop_t&gt; repository;
                  return repository;
              }
  
          void attach(component * c) {
              compipelane.push_back(c);
              c->setparent(this);
          }
  };
  </pre>
  
  <p>In this schema, components will call something like</p>
  
  <p><code>parent.access&lt;double&gt;("health) = 0;</code></p>
  
  <p>Thats fine so far but suppose I want to support dynamic loading of components.
  I know that templates are resolved in compile time, then all code for access()
  calls like the one above will be generated when my engine code is compiled.
  But then I develop a component and compile it as a loadable shared library that
  does an parent.access("damage"), eg:</p>
  
  <pre>class health : public component {
      public:
          health(object & parent) : component(parent) { }
          virtual void tick() {
              double damage = parent.access&lt;double&gt;("damage");
              parent.access&lt;double&gt;("health") -= damage;
              parent.access&lt;double&gt;("damage") = 0;
          }
  }</pre>
  
  <p>Well then begins my sea of questions and doubts: Will this code compile as a
  shared library? If yes, what will happen when I load it at runtime and plug in
  a already created and populated with other components object? Suppose other
  components that already made the access&lt;double&gt;(...) call, will the map be
  the same?</p>
  
  <p>Suppose the component also had a type created, like struct position { int x,y; }
  that was not defined before so this code hasn't been compiled in the main engine,
  what will happen in <code>access&lt;position&gt;(...)</code> in this recently-plugged component?</p>
  
  <p>Sorry about the big question, but its a big question in my head also.
  I am fairly familiar with C++ but still understanding how templates works...</p>
  
  <p>Does anybody have experience with this that could enlighten me?</p>
  
  <p>Also I feel that a static std::map inside a function call is not the best
  approach but I cannot think of another that will let me create spans of
  different property repositories based on a call...</p>
  
  <p>Thank you!</p>