Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shared_ptr requires complete type; cannot use it with lua_State*

I'm writing a C++/OOP wrapper for Lua. My code is:

class LuaState
{
     boost::shared_ptr<lua_State> L;

     LuaState(): L( luaL_newstate(), LuaState::CustomDeleter )
     {
     }
}

The problem is lua_State is incomplete type and shared_ptr constructor requires complete type. And I need safe pointer sharing. (Funny thing boost docs say most functions do not require complete type, but constructor requires, so there is no way of using it. http://www.boost.org/doc/libs/1_42_0/libs/smart_ptr/smart_ptr.htm)

Can can I solve this? Thank you.

like image 708
topright gamedev Avatar asked Mar 12 '10 15:03

topright gamedev


1 Answers

You are using your own deleter, which means that you don't have to have a complete type upon construction. The only requirement is that CustomDeleter can handle that. (it may convert the pointer passed to a complete type, for example (say, from void* to CompleteType*).

The background of completeness is that once the constructor of shared_ptr is called with the default deleter, it will instantiate a class that contains the line delete p; - and for this code to be correct, p must not be incomplete. The destructor will call this deleter code indirectly, so it doesn't depend on the completeness of the type.

However if you pass your own deleter, the requirements of your own deleter will apply. Be sure to define CustomDeleter after lua_State has become completed.

like image 54
Johannes Schaub - litb Avatar answered Nov 14 '22 23:11

Johannes Schaub - litb