Sol2: problem with overload constant member functions

Created on 23 Dec 2016  路  5Comments  路  Source: ThePhD/sol2

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)) );

default

BUT! If the functions defined without 'const ', it works well. So how can we fix it? thanks.

Bike.Shed Feature.Shiny Maybe.Bug...?

All 5 comments

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!

Thank you very much for the perfect solution. The Sol2 is awsome.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrgreywater picture mrgreywater  路  4Comments

RyanSquared picture RyanSquared  路  4Comments

TheMaverickProgrammer picture TheMaverickProgrammer  路  6Comments

Murloc992 picture Murloc992  路  7Comments

morsisko picture morsisko  路  3Comments