【发布时间】:2012-10-03 18:51:55
【问题描述】:
我这个代码,似乎可以工作:
template <typename C>
class class_ {
protected:
std::map<std::string, native_function> methods;
public:
template <typename F, F fn>
class_ &method(const std::string &name) {
methods[name] = method_helper<C, F>::template toNative<fn>();
return *this;
}
};
允许:
class_<MyClass>()
.method<decltype(&MyClass::numRows), &MyClass::numRows>("numRows");
但是,我还希望允许将非成员函数作为方法添加到导出的类中。问题是我需要一个不同的method 定义来使用正常的函数指针:
template <F, F fn>
class_ &method(const std::string &name) {
methods[name] = function_helper<F>::template toNative<fn>();
return *this;
}
但是,如上所示,模板参数将完全相同。
除了创建一个名称完全不同的函数之外,有没有方便的方法来区分函数指针和成员函数指针?或者有没有办法在运行时确定要运行哪些代码?
【问题讨论】:
-
如果我没记错的话,您正在尝试制作某种脚本绑定系统。你知道cpgf吗?它非常像 luabind(我想你知道),但支持 python、lua 和 javascript。
-
我是——CPGF 看起来很有趣,谢谢你的链接。我开始编写这段代码的部分原因是我想更好地理解 c++11 标准,但也因为我想要一个轻量级的包装器。
标签: c++ c++11 function-pointers word-wrap member-function-pointers