Cannot register overloaded member function


#1

I was trying to register an overloaded member function using the example in the documentation and I can’t seem to get it to work:

#include <chaiscript/chaiscript.hpp>

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;

    chai.add(chaiscript::fun(&MyClass::memberdata), "memberdata");
    chai.add(chaiscript::fun(&MyClass::method), "method");
    chai.add(chaiscript::fun(&MyClass::staticmethod), "staticmethod");

    chai.add(chaiscript::fun<void (MyClass::*)()>(&MyClass::overloadedmethod), "overloadedmethod");
    chai.add(chaiscript::fun<void (MyClass::*)(const std::string &)>(&MyClass::overloadedmethod), "overloadedmethod");

    return 0;
}

Generates a bunch of errors:

..\include\chaiscript\dispatchkit\callable_traits.hpp||In instantiation of 'struct chaiscript::dispatch::detail::Callable_Traits<void (MyClass::*)()>':|
C:\Project\include\chaiscript\dispatchkit\register_function.hpp|41|required from 'chaiscript::Proxy_Function chaiscript::fun(const T&) [with T = void (MyClass::*)(); chaiscript::Proxy_Function = std::shared_ptr<chaiscript::dispatch::Proxy_Function_Base>]'|
C:\Project\sandbox\main.cpp|22|required from here|
..\include\chaiscript\dispatchkit\callable_traits.hpp|99|error: 'operator()' is not a member of 'void (MyClass::*)()'|
..\include\chaiscript\dispatchkit\callable_traits.hpp|99|error: 'operator()' is not a member of 'void (MyClass::*)()'|
..\include\chaiscript\dispatchkit\callable_traits.hpp||In instantiation of 'struct chaiscript::dispatch::detail::Callable_Traits<void (MyClass::*)(const std::__cxx11::basic_string<char>&)>':|
C:\Project\include\chaiscript\dispatchkit\register_function.hpp|41|required from 'chaiscript::Proxy_Function chaiscript::fun(const T&) [with T = void (MyClass::*)(const std::__cxx11::basic_string<char>&); chaiscript::Proxy_Function = std::shared_ptr<chaiscript::dispatch::Proxy_Function_Base>]'|
C:\Project\sandbox\main.cpp|23|required from here|
..\include\chaiscript\dispatchkit\callable_traits.hpp|99|error: 'operator()' is not a member of 'void (MyClass::*)(const std::__cxx11::basic_string<char>&)'|
..\include\chaiscript\dispatchkit\callable_traits.hpp|99|error: 'operator()' is not a member of 'void (MyClass::*)(const std::__cxx11::basic_string<char>&)'|

#2

The documentation was a bit outdated. It’s supposed to be:

chai.add(chaiscript::fun<void, MyClass>(&MyClass::overloadedmethod), "overloadedmethod");
chai.add(chaiscript::fun<void, MyClass, const std::string &>(&MyClass::overloadedmethod), "overloadedmethod");

The member function was treated as a regular function.


#3

I suggest you look at the previous discussions on this topic for more details:

and here:

The documentation needs to be updated, the fun<signature> option isn’t really supported anymore. I’m quite surprised tha the example you are giving works. I’ll have to look into it.


#4

The example works just fine

#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>

class MyClass {
  public:
    int memberdata;
    void method() {}
    void method2(int) {}
    static void staticmethod() {}
    void overloadedmethod()
    { printf("overloadedmethod\n"); }
    void overloadedmethod(const std::string & s)
    { printf("overloadedmethod %s\n", s.c_str()); }
};

int main()
{
    chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

    chai.add(chaiscript::user_type<MyClass>(), "MyClass");
    chai.add(chaiscript::constructor< MyClass () >(), "MyClass");

    chai.add(chaiscript::fun(&MyClass::memberdata), "memberdata");
    chai.add(chaiscript::fun(&MyClass::method), "method");
    chai.add(chaiscript::fun(&MyClass::staticmethod), "staticmethod");

    chai.add(chaiscript::fun<void, MyClass>(&MyClass::overloadedmethod), "overloadedmethod");
    chai.add(chaiscript::fun<void, MyClass, const std::string &>(&MyClass::overloadedmethod), "overloadedmethod");

    chai.eval_file("test.chai");

    return 0;
}

Script file:

var t = MyClass();

t.overloadedmethod();
t.overloadedmethod("hello");

print("world");

And it should be normal since I’m just specifying the template parameters manually instead of them being deduced from the static cast. At least that’s that’s what it should be looking at the fun function:

template<typename Ret, typename Class, typename ... Param>
	Proxy_Function fun(Ret (Class::*t_func)(Param...))
	{
		//...
	}

Either way thanks for recommending those links. I could not find them in my search on google.