0

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.

rpatters1
  • 382
  • 1
  • 11
  • It would help if you included the "weird compiler error" in your question. – Brian61354270 Oct 01 '21 at 22:55
  • I included the salient part in the comment in the code. The full error message is "Use 'template' keyword to treat 'addConstructor' as a dependent template name". (I updated the question.) – rpatters1 Oct 02 '21 at 00:12
  • Maybe? I'm still working up to speed on C++1y template programming. I did find that page before posting my question, but I'm afraid I couldn't figure how to apply it to my situation (if it applies). – rpatters1 Oct 02 '21 at 00:32
  • The hard part was figuring out where to put the "template" keyword. I went ahead and associated this question with the long explanation in the link, but the easy answer was to put the "template" keyword after the period but before the "addConstructor": ".template addConstructor()" – rpatters1 Oct 02 '21 at 00:49

0 Answers0