title: C++ Component System Issue
body: <p>I'm trying to get a small <em>Entity/Component System</em> to work in C++. So far it's ok, it doesn't have much to it, but I'm facing an issue.<br>
  Here is my current code:</p>
  
  <pre><code>class Entity {
  public:
      // [...]
      template&lt;typename T, typename ...Args&gt;
      bool AddComponent(Args... args) {
          std::type_index tid = std::type_index(typeid(T));
          if (_Components[tid] == nullptr) {
              _Components[tid] = std::make_shared&lt;T&gt;(args...);
              return true;
          }
          return false;
      }
  
      template&lt;typename T&gt;
      std::shared_ptr&lt;T&gt; GetComponent() {
          // Issue here
          std::type_index tid = std::type_index(typeid(T));
          return std::dynamic_pointer_cast&lt;T&gt;(_Components[tid]); // return nullptr if no component
      }
  
      template&lt;typename T&gt;
      bool RemoveComponent() {
          std::type_index tid = std::type_index(typeid(T));
          if (_Components.erase(tid) == 1) return true;
          return false;
      }
  
      private:
          std::map&lt;std::type_index, std::shared_ptr&lt;Component&gt;&gt; _Components;
  };
  </code></pre>
  
  <p>As seen above, it's pretty straightforward, each component is derived from a base class <code>Component</code> and is used as follow:</p>
  
  <pre><code>class Component;
  class MyComponent: public Component {
      // [...]
      MyComponent(int foo_): foo(foo_) {}
      int foo;
  }
  
  Entity e;
  e.AddComponent&lt;MyComponent&gt;(42);
  std::cout &lt;&lt; e.GetComponent&lt;MyComponent&gt;()-&gt;foo; // 42
  e.RemoveComponent&lt;MyComponent&gt;();
  std::cout &lt;&lt; e.GetComponent&lt;MyComponent&gt;()-&gt;foo; // Error
  </code></pre>
  
  <p>The error is logic and I understand why, but my question would be how to prevent a error when calling a <code>Component</code> that has been removed, and thus is <code>nullptr</code> without crashing the program ?</p>
  
  <p>Is a simple try/catch enough ? How should it be implemented here ?</p>