title: C++ Push Multiple Types onto Vector
body: <p><strong>Note:</strong> I know similar questions to this have been asked on SO before, but I did not find them helpful or very clear.</p>
  
  <p><strong>Second note:</strong> For the scope of this project/assignment, I'm trying to avoid third party libraries, such as Boost.</p>
  
  <p>I am trying to see if there is a way I can have a single vector hold multiple types, in each of its indices. For example, say I have the following code sample:</p>
  
  <pre><code>vector&lt;something magical to hold various types&gt; vec;
  int x = 3;
  string hi = "Hello World";
  MyStruct s = {3, "Hi", 4.01};
  
  vec.push_back(x);
  vec.push_back(hi);
  vec.push_back(s);
  </code></pre>
  
  <p>I've heard <code>vector&lt;void*&gt;</code> could work, but then it gets tricky with memory allocation and then there is always the possibility that certain portions in nearby memory could be unintentionally overridden if a value inserted into a certain index is larger than expected.</p>
  
  <p>In my actual application, I know what <em>possible</em> types may be inserted into a vector, but these types do not all derive from the same super class, and there is no guarantee that all of these types will be pushed onto the vector or in what order.</p>
  
  <p>Is there a way that I can <strong>safely</strong> accomplish the objective I demonstrated in my code sample?</p>
  
  <p>Thank you for your time.</p>