1

In the question How to register member function to lua without lua bind in c++ one answer suggested the following code:

class C {
   public:
      void blah(lua_State* L);
};

C inst;

lua_pushcclosure(L, std::bind(&C::blah, &inst, std::placeholder::_1), 0);
lua_setglobal(L, "blah");

(Quoted as it stood, including the small error in std::placeholders)

However, I cuold not get that to work. The error message I got back states that the function returned by std::bind can't be converted to a lua_CFunction.

I have also tried changing the return type of blah to int, but I get the same error message. If it's helpful to anyone, the full error message is:

Error   C2664   'void lua_pushcclosure(lua_State *,lua_CFunction,int)': cannot convert argument 2 from 'std::_Binder<std::_Unforced,int (__thiscall C::* )(lua_State *),C *,const std::_Ph<1> &>' to 'lua_CFunction'

I even tried to change &C::blah to &inst.blah, but that unsurprisingly didn't work either.

Has anyone gotten it to work? Or is it just not meant to work?

Community
  • 1
  • 1
kim
  • 41
  • 7
  • 3
    *I cuold not get that to work.* You are not supposed to. The referenced answer is plainly wrong. – n. m. could be an AI Apr 09 '17 at 17:40
  • 1
    should not your function have a signature of `int blah(lua_State* L)` – macroland Apr 09 '17 at 23:50
  • Yes @macroland, you're right. I have tried that as well, though, and still no luck. – kim Apr 11 '17 at 13:45
  • You might want to read this: http://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions – macroland Apr 12 '17 at 04:15
  • Thank you @macroland - that was very informative and helped me understand `std::bind` better. However, I'm still puzzled as to whether that means `std::bind` cannot be used with `lua_pushcclosure` or whether there is a way around it? – kim Apr 12 '17 at 09:47
  • Frankly, I havent tried `std::bind` to glue C++ and Lua. Why not add C and C++ to your tags to be able to get some more opinions. – macroland Apr 12 '17 at 23:54
  • @JanHudec thank you for your reply! However, I don't really understand what I need to do. Would you mind giving more information or even an example? Maybe even as an answer to the question? – kim Apr 25 '17 at 07:20
  • 1
    @JanHudec no need to be so rude! You claim that "`std::bind` _could_ be used to create a lua closure" so excuse me if I mistook that for you actually knowing anything about the subject. Clearly you miss the point of my question - the reply I'm referencing (**and you**, in your comment) claims that `std::bind` can be used - my question is, _is that correct or false and if it's correct, then how do I get it to work_? Your comment is utterly unhelpful. – kim Apr 25 '17 at 07:48
  • @kim, ok **comment deleted**. – Jan Hudec Apr 25 '17 at 08:31
  • 1
    You can't. Lua expects a C function. You're handing it an object with an overloaded operator `()`. Huge difference. – Qix - MONICA WAS MISTREATED Apr 25 '17 at 09:40

0 Answers0