Search code examples
c++staticlualuabind

luabind and static fields


I'm trying to export static fields from class:

class Foo
{
   const static int Var;
};

// luabind module:
.def_readonly("Var", &Foo::Var);
// I've also tried
.def_readonly("Var", Foo::Var);
 error: no matching function for call to ‘luabind::class_<Foo>::def_readonly(const char [6], const Foo&)’
 note: template<class C, class D> luabind::class_& luabind::class_::def_readwrite(const char*, D C::*)

What have I missed up?


Solution

  • As clearly stated in the documentation, static functions (among other things) can't be added as members. They have to be scoped in a special .scope construct.

    class_<foo>("foo")
        .def(constructor<>())
        .scope
        [
            class_<inner>("nested"),
            def("f", &f)
        ];
    

    I don't know if the non-member function version of def has readonly versions for variables, but it may. If it doesn't, then you'll have to expose it as a function that returns the value.