title: Container of pointers to template class
body: <p>I'm trying to make a class (<code>ParametersHolder</code>) that can contain different named parameters (<code>Parameter&lt;T&gt;</code>). The <code>ParametersHolder</code> has an <code>std::unordered_map</code> container holding my parameters. To make this work, I had to derive <code>Parameter&lt;T&gt;</code> from an empty <code>ParameterBase</code> class and I dynamically cast <code>ParameterBase</code> to <code>Parameter&lt;T&gt;</code> when I want to get a parameter with the right type. Here is the code:</p>
  
  <p><strong>ParametersHolder.hpp</strong></p>
  
  <pre><code>...
  #include "Parameter.hpp"
  
  class ParametersHolder {
  
    std::unordered_map&lt;std::string, std::shared_ptr&lt;ParameterBase&gt;&gt; parameters;
  
    public:
  
      bool paramExists(std::string name) {
        return parameters.find(name) != parameters.end();
      }
  
      template&lt;typename T&gt;
      std::shared_ptr&lt;Parameter&lt;T&gt;&gt; getOrCreate(std::string name, int dims = 1) {
        if (paramExists(name)) {
          return getParam&lt;T&gt;(name);
        }
        return create&lt;T&gt;(name, dims);
      }
  
      template&lt;typename T&gt;
      std::shared_ptr&lt;Parameter&lt;T&gt;&gt; create(std::string name, int dims = 1) {
        if (paramExists(name)) throw std::runtime_error("Trying to create parameter '" + name + "' but it already exists");
        auto param = std::make_shared&lt;Parameter&lt;T&gt;&gt;(dims);
        parameters[name] = param;
        return param;
      }
  
      template&lt;typename T&gt;
      std::shared_ptr&lt;Parameter&lt;T&gt;&gt; getParam(std::string name) {
        if (!paramExists(name)) throw std::runtime_error("Parameter '" + name + "' doesn't exist");
        auto param = std::dynamic_pointer_cast&lt;Parameter&lt;T&gt;&gt;(parameters[name]);
        if (param == nullptr) throw std::runtime_error("Parameter '" + name + "' is not a(n) " + boost::typeindex::type_id&lt;T&gt;().pretty_name());
        return param;
      }
  
  };
  </code></pre>
  
  <p><strong>Parameter.hpp</strong></p>
  
  <pre><code>class ParameterBase {
    public:
      virtual ~ParameterBase() {}
  };
  
  template&lt;typename T&gt;
  class Parameter : public ParameterBase {
  
    std::vector&lt;T&gt; data;
  
    public:
  
      explicit Parameter(int dim = 1) : data(dim) {}
  
      T getValue(int dim = 1) {
        dimCheck(dim);
        return data[dim - 1];
      }
  
      void setValue(T value, int dim = 1) {
        dimCheck(dim);
        data[dim - 1] = value;
      }
  
    private:
      void dimCheck(int dim) {
        if (dim &gt; data.size()) {
          auto errorMsg = "Trying to access dimension " + std::to_string(dim)
            + " but parameter has only " + std::to_string(data.size()) + " dimensions";
  
          throw std::runtime_error(errorMsg); 
        }
      }
  };
  </code></pre>
  
  <p>To use this code I can do that:</p>
  
  <pre><code>auto paramsHolder = std::make_shared&lt;ParametersHolder&gt;();
  auto filename = paramsHolder-&gt;create&lt;std::string&gt;("filename");
  filename-&gt;setValue("untitled.txt");
  
  ...
  
  auto param = paramsHolder-&gt;getParam&lt;std::string&gt;("filename");
  std::cout &lt;&lt; param-&gt;getValue() &lt;&lt; std::endl;
  </code></pre>
  
  <p>This works fine but having an empty <code>ParameterBase</code> feels odd. Is there another way to achieve that? I read about <code>boost::any</code>, but I'm not sure this is the right use case. And if it is, I don't know what the type signature of the <code>unordered_map</code> should be among these:</p>
  
  <pre><code>std::unordered_map&lt;std::string, std::shared_ptr&lt;Parameter&lt;boost::any&gt;&gt;&gt;
  std::unordered_map&lt;std::string, std::shared_ptr&lt;boost::any&gt;&gt;
  std::unordered_map&lt;std::string, boost::any&gt;
  </code></pre>