EDIT: I am using XCode 12.5.1 with GCC. This issue may be limited to GCC because MSC is compiling it without errors. Auggh.
I am trying to set up generic template functions to add classes to LuaBridge. I am getting a weird error on .addConstructor
. Ultimately I am considering refl-cpp
to get type names and function info, but for now I am struggling with this issue that has nothing to do with refl-cpp
.
Here is my template function:
struct foo {int bar();};
template<class T, typename ConstructorArgs>
void addclass(lua_State *l)
{
luabridge::getGlobalNamespace(l)
.beginNamespace ("bar")
.beginClass<T>("foo")
.addConstructor<ConstructorArgs>() // error: Use 'template' keyword to treat 'addConstructor' as a dependent template name
.addFunction("bar", &foo::bar)
.endClass()
.endNamespace();
}
static void my_program()
{
lua_State *l;
l = luaL_newstate();
luaL_openlibs(l);
addclass<foo, void (*) (void)>(l);
}
Note that the .addFunction
call has no problem. The program compiles and runs correctly if I comment out the .addConstructor
. The weird part is, if I ignore the T
template class and hardcode foo
in the call to beginClass
, it works:
template<class T, typename ConstructorArgs>
void addclass(lua_State *l)
{
luabridge::getGlobalNamespace(l)
.beginNamespace ("bar")
.beginClass<foo>("foo") // hardcoding <foo> here eliminates error and program works
.addConstructor<ConstructorArgs>()
.addFunction("bar", &foo::bar)
.endClass()
.endNamespace();
}
I can't really tell if the problem is in addConstructor
or beginClass
.
EDIT: Check out the linked article below. Based on that, the short answer is to change the call to addConstructor
as follows:
.template addConstructor<ConstructorArgs>()
This works on both MSC and GCC.