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

标签 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");

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

最佳答案

你写道:

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>)
//                                                  ~~~~~~~~~~~

关于c++ - 如何在ChaiScript 中注册重载的模板成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101399/

相关文章:

c++ - 为什么多线程中的 Local/Stack 变量不需要锁定

c++ - C++ 应用程序如何使用比 RSS 内存更多的堆?

c++ - 在 CLion 中使用 OpenGL 时出现未定义引用错误

C++内部变量的奇怪模板特化

c++ - 将代码从 gcc 移植到 clang

c++ - 自动修复 C 中缺少的括号

c++ - 为什么显式模板实例化会在存在外线虚拟时导致 weak-template-vtables 警告?

c++ - 右值在不使用 std::move 的情况下导致警告

c++ - 对带有可变参数模板的 std::ref() 和 std::bind() 有点模糊

c++ - 更高类型的别名模板