通过 boost::function_base 调用 boost::function
我有一个 unordered_map 函数,在解析 XML 文件时应该在对象上调用该函数。
我发现 boost::function 有一个名为 boost::function_base 的基类,但是正如预期的那样,我无法调用它,因为我没有该函数的签名。
由于所有这些函数都是 setter 函数,我可以保证它们返回 void 并且只有一个未知类型的参数。
除了我试图避免的 if-else-if 分支之外,还有其他更好的方法来解析类型吗?
I have an unordered_map of functions that should be called on an object when an XML file is parsed.
I have found that boost::function has a base class named boost::function_base, however as expected I cannot invoke it because I don't have the signuture of the function.
Since all of those functions are setter functions, I can guarantee that they return void and have only one parameter of an unknown type.
Is there any better way to resolve the type other then an if-else-if branch which I am trying to avoid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 boost::variant 是最好的方法。无论如何,你怎么可能调用一个参数类型未知的函数呢?
Use a boost::variant is the best way to go. How could you possibly invoke a function with an unknown parameter type, anyway?
boost::function
仅为编译时多态性而设计。为什么不直接使用常规函数指针呢? IEboost::function
is designed for compile time polymorphism only. Why don't you just use a regular function pointer? I.e.