如何在ChaiScript中注册重载的模板成员函数?

时间:2017-03-29 18:30:58

标签: c++ templates c++14 overloading chaiscript

我有下一个定义类:

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

如何在ChaiScript v6.0.0中注册重载模板成员函数MyType::put的正确方法?

我正在尝试专门的价值模板(讨论于: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");

但是因为有几个候选函数而没有编译。

1 个答案:

答案 0 :(得分:1)

您写道:

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

但是如果你看一下你的第一个成员函数,它的签名是:

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

如果Tuint64_t,则第二个参数不是uint64_t。它是uint64_t&&。因此,你想要的是:

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