Search code examples
c++templatesc++14overloadingchaiscript

How register overloaded template member function in ChaiScript?


I have the next definition class:

class MyType {
public:
    template <typename T>
    MyType& put(const std::string& s, T&& val);

    template <typename T>
    MyType& put(size_t pos, T&& val);

    template <typename M, typename T, typename = std::enable_if_t<std::is_same<MyType, std::decay_t<M>>::value>>
    MyType& put(const M& o, T&& val);
}

How is the right way to register overloaded template member functions MyType::put in ChaiScript v6.0.0?

I am trying specialized value template (Discussed in: http://discourse.chaiscript.com/t/issues-with-adding-templated-and-or-overloaded-operators/19/3):

chai.add(chaiscript::fun(static_cast<MyType& (MyType::*)(const std::string&, uint64_t)>(&MyType::put<uint64_t>), "put");

But not compile because there several candidate function.


Solution

  • You wrote:

    static_cast<MyType& (MyType::*)(const std::string&, uint64_t)>(
        &MyType::put<uint64_t>)
    

    But if you look at your first member function, its signature is:

    template <typename T>
    MyType& put(const std::string& s, T&& val);
    

    If T is uint64_t, then the second argument isn't uint64_t. It's uint64_t&&. Hence, what you want is:

    static_cast<MyType& (MyType::*)(const std::string&, uint64_t&&)>(
        &MyType::put<uint64_t>)
    //                                                  ~~~~~~~~~~~