I'm learning luabind and attempting to use luabind::object to access variables in Lua from C++.
When I assigned an int to an "object", the compilation failed.
The Code:
int main()
{
using namespace luabind;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
open(L);
luaL_dofile(L, "./test.lua"); // a = 5
object g = globals(L);
object num = g["a"];
num = 6; // Failed to compile
return 0;
}
The error messages:
luatets.cpp:21:6: error: no match for ‘operator=’ (operand types are ‘luabind::adl::object’ and ‘int’)
num = 6;
/usr/include/luabind/object.hpp:731:9: note: luabind::adl::object& luabind::adl::object::operator=(const luabind::adl::object&)
class object : public object_interface<object>
/usr/include/luabind/object.hpp:731:9: note: no known conversion for argument 1 from ‘int’ to ‘const luabind::adl::object&’
But after I combined the two lines, the code worked:
g["a"] = 6;
I don't know why this happened. In luabind documentation, it's said that:
When you have a Lua object, you can assign it a new value with the assignment operator (=). When you do this, the default_policy will be used to make the conversion from C++ value to Lua.
And in the class declaration, the assignment operator is overloaded for arbitrary types, not only for object&:
template <class T>
object& operator=(T const&);
object& operator=(object const&);
By the way, I found my problem similar to this, but nobody answered it.
I've had a look at the luabind headers, but could not find any clue inside those horrible proxies.
Could anyone tell me why the first code is not correct and whether the operator=(T const &)
is overloaded ?
Thanks a lot!!
Luabind 0.9.1 has no such operator= overload in object, regardless of what the (probably outdated?!) documentation says. In fact, object has no operator= overloads at all (and neither has object_interface, which object is derived from).
However, it has:
Which is why
g["a"] = 6;
worked since index_proxy has:which can be instantiated for int.