title: Problems with casting a void** to a T**
body: <p>TL;DR: I have a Derived** that I store as a in Lua as a void* userdata. Then I try to get it back as a Base** and stuff breaks. Is there anything I can do or is this all madness that's doomed to failure?</p>
  
  <p>Details:</p>
  
  <p>I'm passing some data back and forth between Lua and C++, and Lua requires the use of void* to store userdata (That I'm using Lua isn't too important, other than that it uses void pointers). Makes sense so far. Lets say I have three classes, <code>Base</code> and <code>Derived</code>, with <code>Derived</code> inheriting from <code>Base</code>. The userdata I feed to Lua is a pointer to a pointer, like so:</p>
  
  <pre><code>template &lt;typename T&gt;
  void lua_push(L, T* obj) {
      T** ud = (T**)lua_newuserdata(L, sizeof(T*)); // Create a new userdata
      *ud = obj; // Set a pointer to my object
      // rest of the function setting up other stuff omitted
  }
  </code></pre>
  
  <p>Of course, this is in a nice templated function, so I can pass any of my three types in this way. Later on I can use another templated function to get my userdata out of Lua, like so: </p>
  
  <pre><code>template &lt;typename T&gt;
  T* lua_to(lua_State* L, int index) { 
      // there's normally a special metatable check here that ensures that 
      // this is the type I want, I've omitted it for this example
      return *(T**)lua_touserdata(L, index);
  }
  </code></pre>
  
  <p>This works fine when I pass in and out the same type. I'm running into a problem though when trying to pull a <code>Derived</code> out as a <code>Base</code>. </p>
  
  <p>In my specific case, I have a vector being stored on <code>Base</code>. I use <code>lua_push&lt;Derived&gt;(L, obj);</code> to push my object to Lua. Later, in another place I pull it out using <code>Base* obj = lua_to&lt;Base&gt;(L, i);</code>. I then <code>push_back</code> some stuff into my vector. Later on, another portion of code pulls out that exact same object (verified with pointer comparisons) except this time uses <code>Derived* obj = lua_to&lt;Derived&gt;(L, i);</code> My <code>Derived</code> object doesn't see that object that was pushed in. I believe I've narrowed this down to incorrect casting, and I'm probably corrupting some memory somewhere when I make my call to <code>push_back</code></p>
  
  <p>So my question is, is there a way to make that cast work right? I've tried the various flavors of casts. static_cast, dynamic_cast and reinterpret_cast don't seem to work, either giving me the same wrong answer or not compiling at all. </p>
  
  <p>Specific example:</p>
  
  <pre><code>Base* b = lua_to&lt;Base&gt;(L, -1); // Both lua_to's looking at the same object
  Derived* d = lua_to&lt;Derived&gt;(L, -1); // You can be double sure because the pointers in the output match
  std::cout &lt;&lt; "Base: " &lt;&lt; b &lt;&lt; " " &lt;&lt; b-&gt;myVec.size() &lt;&lt; std::endl;
  std::cout &lt;&lt; "Derived: " &lt;&lt; d &lt;&lt; " " &lt;&lt; d-&gt;myVec.size() &lt;&lt; std::endl;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Base: 0xa1fb470 1
  Derived: 0xa1fb470 0
  </code></pre>