title: boost::variant and void* pointers
body: <p>I need a variant type that holds instances of any user-defined class. So I use void*:</p>
  
  <pre><code>typedef boost::variant&lt;void*, int, float, std::string&gt; Tvariant;
  </code></pre>
  
  <p>I've created a wrapper class with a map:</p>
  
  <pre><code>typedef std::map&lt;std::string, Tvariant&gt; Tvalues;
  </code></pre>
  
  <p>Example of usage:</p>
  
  <pre><code>int x = 123;
  attributes.set("int_var", x);
  x = attributes.get&lt;int&gt;("int_var");
  
  MyClass* obj = new MyClass();
  attributes.set("void*_var", obj);
  obj = static_cast&lt;MyClass*&gt;( attributes.get&lt;void*&gt;("void*_var") );
  obj = attributes.cast&lt;MyClass*&gt;("void*_var"); // the same
  </code></pre>
  
  <p>There are 2 issues with this void* in variant class:</p>
  
  <ol>
  <li>Copying attributes with pointers to dynamically allocated memory is dangerous and error-prone.</li>
  <li>User can static_cast void* to WrongClass*, not to MyClass*. It compiles, but result is unpredicted.</li>
  </ol>
  
  <p>Possible solutions:</p>
  
  <ol>
  <li>Use boost::shared_ptr&lt; void* >.</li>
  <li>Remember typeid for all void* values (when they are added) in the <code>std::map&lt;void*, typeid&gt; typeid_map</code>. When user requests for void* value, casting it to any TClass*, let's evaluate an assertion: <code>assert(typeid_from_typeid_map == typeid(TClass*))</code>.</li>
  </ol>
  
  <hr>
  
  <p><strong>Questions</strong>:<br>
  1. Are there other solutions to hold values of any user-defined class?<br>
  2. May be you will recommend better solutions for mentioned issues and point some other issues?  </p>