satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

Support for Template Constructor / Members / Functions #27

Closed Sygmei closed 8 years ago

Sygmei commented 8 years ago

Do you plan to add support for templates when adding constructor, members or functions to a ClassMetatable ? It works when you specify the type but could it be possible to leave the type empty and let Lua use any type ?

satoren commented 8 years ago

I'm sorry, i dont understand. Can you write example code?

Sygmei commented 8 years ago

For example if we have a simple function which add two things : template<typename V> V addStuff(V a, V b) { return a + b; }

That I can use like the following : int a = addStuff(3, 3); std::string b = addStuff((std::string)"foo", (std::string)"bar"); std::cout << a << std::endl; //Display 6 std::cout << b << std::endl; //Display foobar

I can't do that in kaguya : kaguya::function(addStuff);

I can do this : kaguya::function(addStuff<int>);

But I'll have to specify each type I want to use, is it possible to pass the entire function and use all the possible types in Lua ? Either by using kaguya::templatedFunction (or something like this), or some overloading of kaguya::function (Same for addMember, addMemberFunction etc..). I don't know if C++ allows to do this.

satoren commented 8 years ago

Can not it. Instantiate of C++ template is compile time only. When you invoke function from Lua, type determine runtime.

That you can is below only.

kaguya::overload(
//List the type that might be used
    addStuff<int>,
    addStuff<std::string>,
    addStuff<my_class>,
    addStuff<my_other_class>);
Sygmei commented 8 years ago

I understand :) Thanks anyway !