通过 boost::function_base 调用 boost::function

发布于 2024-09-18 14:48:37 字数 238 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2024-09-25 14:48:37

使用 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?

耳钉梦 2024-09-25 14:48:37

boost::function 仅为编译时多态性而设计。为什么不直接使用常规函数指针呢? IE

typedef void (*function_type)(void *);

boost::function is designed for compile time polymorphism only. Why don't you just use a regular function pointer? I.e.

typedef void (*function_type)(void *);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文