0

我有下一个定义类:

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

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

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

但不能编译,因为有几个候选函数。

4

1 回答 1

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>)
//                                                  ~~~~~~~~~~~
于 2017-03-29T18:37:50.740 回答