How to register an overloaded member function?


#1

I’m trying to register an overloaded member function, using the example in the documentation:

Chaiscript: 5.8.3

GCC: gcc version 5.3.1 20160519 (Debian 5.3.1-20)

class MyClass {
public:
int memberdata;
void method();
void method2(int);
static void staticmethod();
void overloadedmethod();
void overloadedmethod(const std::string &);
};

int main() {
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
chai.add(fun(&MyClass::memberdata), “memberdata”);
chai.add(fun(&MyClass::method), “method”);
chai.add(fun(&MyClass::staticmethod), “staticmethod”);
chai.add(fun< void (MyClass::)()>(&MyClass::overloadedmethod), “overloadedmethod”);
chai.add(fun< void (MyClass::
)(const std::string &)>(&MyClass::overloadedmethod), “overloadedmethod”);
}

Errors:

dani@debian:~/workspace/chai_test$ g++ -std=c++11 main.cpp -lpthread -ldl
In file included from include/ChaiScript/include/chaiscript/dispatchkit/proxy_functions_detail.hpp:19:0,
from include/ChaiScript/include/chaiscript/dispatchkit/proxy_functions.hpp:24,
from include/ChaiScript/include/chaiscript/dispatchkit/proxy_constructors.hpp:11,
from include/ChaiScript/include/chaiscript/dispatchkit/dispatchkit.hpp:31,
from include/ChaiScript/include/chaiscript/chaiscript.hpp:822,
from main.cpp:1:
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp: In instantiation of ‘struct chaiscript::dispatch::detail::Callable_Traits<void (MyClass::)()>’:
include/ChaiScript/include/chaiscript/language/…/dispatchkit/register_function.hpp:41:72: required from ‘chaiscript::Proxy_Function chaiscript::fun(const T&) [with T = void (MyClass::
)(); chaiscript::Proxy_Function = std::shared_ptrchaiscript::dispatch::Proxy_Function_Base]’
main.cpp:19:74: required from here
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp:99:56: error: ‘operator()’ is not a member of ‘void (MyClass::)()’
typedef typename Function_Signature<decltype(&T::operator())>::Signature Signature;
^
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp:99:56: error: ‘operator()’ is not a member of ‘void (MyClass::
)()’
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp: In instantiation of ‘struct chaiscript::dispatch::detail::Callable_Traits<void (MyClass::)(const std::__cxx11::basic_string&)>’:
include/ChaiScript/include/chaiscript/language/…/dispatchkit/register_function.hpp:41:72: required from ‘chaiscript::Proxy_Function chaiscript::fun(const T&) [with T = void (MyClass::
)(const std::__cxx11::basic_string&); chaiscript::Proxy_Function = std::shared_ptrchaiscript::dispatch::Proxy_Function_Base]’
main.cpp:20:93: required from here
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp:99:56: error: ‘operator()’ is not a member of ‘void (MyClass::)(const std::__cxx11::basic_string&)’
include/ChaiScript/include/chaiscript/dispatchkit/callable_traits.hpp:99:56: error: ‘operator()’ is not a member of ‘void (MyClass::
)(const std::__cxx11::basic_string&)’

I’m compiling with std=c++11. maybe the documentation is wrong, but how can we register overloaded member functions?

Thank you!!


#2

The:

fun<type>(&function)

version is a bit finicky and probably needs work. It used to work better but I simplified some code perhaps a bit too much. You have two options that definitely will work:

// use static cast
chai.add(fun(static_cast<void (MyClass::*)()>(&MyClass::overloadedmethod)), "overloadedmethod");
// Use a lambda 
chai.add(fun([](MyClass &o) { return o.overloadedmethod(); }), "overloadedmethod");

The lambda version can give you lots of flexibility for rearranging parameters / defaulting them / etc.

-Jason


#3

Yes, the static_cast version works now.

Thank you!


#4

One more question. If we use the lambda version, will it have some performance implications instead of using the static_cast version?

thanks again!


#5

In debug builds it may have a minor overhead cost, in release builds the lambda version should mostly or completely just go away. Almost certainly not a measurable difference in either case.