lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Small notes on C++ syntax:

> int factory(lua_State* L)
> {
>  T* object = new (reinterpret_cast<T*>(lua_newuserdata(L, sizeof(T)))) T;
>  /* set metatable here */
>  return 1;
> }

The cast is useless here. The following form would work too :
T* object = new (lua_newuserdata(L, sizeof(T))) T;

> int gc_metamethod(lua_State* L)
> {
>  T* object = reinterpret_cast<T*>(lua_touserdata(L, 1));
>  object->~T();
>  return 0;
> }

It would be better to use static_cast.
This is a essentially a matter of taste, but static_cast is for
regular (mostly safe) casts, const_cast to work around when an API
does not respect correct constness, dynamic_cast to try types at
runtime, and reinterpret_cast is for "hacks". When I see it, it
triggers this thought : "Hey, there is something tricky going on
here".