I have two constant member functions in my 'spin_system' class.
SpMat op(const string& list, op_side type = kComm) const ;
SpMat op(const sol::table& t, op_side type = kComm) const ;
I found that the functions can't be resolved as follows:
lua.new_usertype<spin_system>("spin_system",
sol::constructors<sol::types<string>, sol::types<sol::table>>(),
"op", sol::overload(
sol::resolve<SpMat(const string&, op_side)>(&spin_system::op),
sol::resolve<SpMat(const sol::table&, op_side)>(&spin_system::op))
);

BUT! If the functions defined without 'const ', it works well. So how can we fix it? thanks.
Also, there is another problem with my member functions. Note that the second parameter has a default value 'kComm', but in the lua script we have to always assign all the two parameters explicitly.
local Iz1=sys1:op("1, I-",0)
local Iz2=sys1:op({"1","I-"},0)
Any one can help me? Thanks a lot.
You cannot bind "default parameters". They exist solely at compile-time in C++ and the only way to mimic that would be to use deliberate overloading with sol::overload, that is you'd have to add 2 more overloads using a lambda or something to the list you have that take 1 argument and pass it to the C++ function.
The first problem seems like a bug on my end, but I'm not able to figure out a way around the template shenanigans required to capture this.
Therefore, I recommend that you use a static_cast<SpMat (spin_system::* )(const std::string&, op_side)const>( &spin_system::op );
Derp. I'm a dumby. All you need to solve this problem is:
sol::resolve<SpMat(const std::string&, op_side) const>( &spin_system::op );
Note the const at the end just before the close of the sol::resolve<...> template bit. You have to specify that to make it work. I'm going to add a note and example to the docs so future people (and myself) don't get all tripped up on this.
Thanks for reporting!
Fixed. http://sol2.readthedocs.io/en/latest/api/resolve.html
Pull from the latest to get what you need or grab the single: https://github.com/ThePhD/sol2/blob/develop/single/sol/sol.hpp
Thank you very much for the perfect solution. The Sol2 is awsome.