davidsiaw / luacppinterface

A Simple C++ Interface to Lua
MIT License
168 stars 28 forks source link

Create LuaUserdata from template #32

Closed tesla9 closed 8 years ago

tesla9 commented 8 years ago

hi, maybe I understand something incorrectly, but I am unable to create LuaUserdata from template class.

Here is the example: class:

template <typename T>
class TestClass
{
    T mA;
public:
    TestClass(T a): mA(a) {}
};

code:

LuaTable testClass = mLua.CreateTable();
auto makeTestClass = lua.CreateFunction< LuaUserdata<TestClass<int>(int)> >(
[&](int prm)
{
LuaUserdata<TestClass<int>> newTestClass = mLua.CreateUserdata<TestClass<int>>(new TestClass<int>(prm));
return newTestClass;
}
);
testClass.Set("new", makeTestClass);

and error: error: no instance of function template "CreateFunction" matches the argument list argument types are: (lambda ->LuaUserdata<TestClass>) object type is: Lua

Is there any other way to do that? When TestClass is not a template everything works perfect.

davidsiaw commented 8 years ago

I assume you wanted a function that takes an int and returns a LuaUserdata< TestClass<int> >

In this case your function signature is wrong. Instead of

LuaUserdata< TestClass<int>(int) >

it should be

LuaUserdata< TestClass<int> >(int)

(note the triangular brackets' placements)

tesla9 commented 8 years ago

Thank you very much David, I had to be tired ;)