问题标签 [boost-any]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - boost::any 的访问者模式
我找到了这个https://gist.github.com/2945472但我需要一个不依赖于 c++11 的实现。我尝试将其转换为仅使用 boost,但我遇到了一些麻烦。
这是我想出的:
插入地图时,我得到了 bad_any_cast。不应该 any_cast 仅由 it->second(x) 调用吗?我究竟做错了什么?
c++ - boost::any number cast
我把正的'int'放在我的任何变量上。然后我试图将它转换为'unsigned long'。我有一个关于糟糕的演员阵容的例外。我如何可以更灵活地使用 'boost::any_cast' 来考虑整数数据。
c++ - 如果我知道 gdb 的类型,如何查看 boost::any 的值
我有一个核心转储,我正在使用 gdb 查看核心转储。
我想知道是否有一种方法可以检查 gdb 中 boost::any 值的值?
在核心,我有任何提升的地址,所以我尝试将其转换为占位符,看看我是否可以检查这个值,但我没能做到。我知道 boost any 的类型是 unsigned long 那么有没有办法查看知道类型的 any 值?
对此问题的任何帮助将不胜感激。谢谢!
c++ - C++ 性能:模板与 boost.any
我想知道是否在任何可以使用模板的地方使用 boost.any(没有 RTTI)类会减慢程序的速度。由于 boost any 实际上是模板类的包装器,因此可以说使用现代编译器优化它会产生相同的效果,对吗?
tpl_vs_any.hpp
那么这样说是否正确:
完全一样快:
假设您知道,就像编译器在第二个示例中所做的boost::any
那样,在这种情况下仅用作 double 吗?(任何类都没有 RTTI)。
我更想知道支持和反对这个论点的论点。
此外,这些方法之间是否存在差异的特定情况?
编辑: 性能测试 2:
- 示例 1:1,966.57 毫秒
- 示例 2:1,320.37 毫秒
似乎有一个相对较大的差异。
编辑2:
由于将主要数据类型double
与类进行比较是不公平的,因此any
我进行了新测试:
速度:1,794.54 毫秒
速度:1,715.57 毫秒
多次测试,几乎相同的基准。
c++ - 是否可以在没有 RTTI 的情况下使用 Boost Program Options?
我想在我的项目中禁用 RTTI。但是,该项目使用 Boost Program Options,它本身依赖于不支持 -fno-rtti 的 Boost Any。
我想知道是否有任何解决方案可以在没有 RTTI 的情况下使用 Boost Program?通过定义宏还是不使用某些功能?
谢谢
c++ - boost::any and templates
I'm writing a library which involves a good amount of both template trickery and boost::any. I've run into a situation where I essentially have this:
... and I need to call a function which looks like this:
I could resort to an obscenely nested series of if statements, but assuming I'm handling 10 distinct types, that's 10,000 if statements! Boost preprocessor could help here, but this is still a horrible solution.
Is there a better way to call a templated function with the contents of a boost::any without resorting to this sort of madness? As far as I can tell, there is not.
c++ - 使用 boost::any 是个好主意吗?
我正在用 C++ 开发一些图像处理算法。为了使我的代码更通用并且能够在不重新编译整个项目的情况下配置所有内容,我想出了一个想法,将处理算法分成小部分(“提取器”),将它们作为从单个接口继承的对象和从工厂方法解析的 XML 文件中配置它们的执行顺序和参数。但是这些基本处理块的输入和输出类型可以不同,所以我考虑使用 boost::any 作为通用类型,所以每个对图像的操作看起来像:
每个对象都应在内部存储正确的输入和输出类型,并在每次执行时执行装箱-拆箱。使用这种技术是个好主意吗?它满足了我的需求,并且在 Python 中非常自然,但同时在 C++ 中看起来像一个丑陋的 hack,它本质上是静态类型的,所以我怀疑是否应该使用它。
UPD:一个更清楚的小例子
c++ - Is it possible to avoid overhead while using boost_any?
I'd like to use boost::any
as a universal type to store in a container and to pass to different functions. But inside these functions I always know the real type, so in runtime I don't need any type safety checks and the imposed overhead, static_cast
is enough.
The ideal solution would be to use something like boost::polymorphic_downcast
, but as I can see it can't be applied in this case.
Should I just write my own wrapper for void*
or is there another option?
boost - Boost MSM,尽管 boost::any 被用作事件,但不会发生转换
我正在使用 boost MSM 框架开发状态机。他们的教程指出boost::any可以用作“Kleene 事件”,如果当前状态是源状态,则允许在任何被触发的事件上进行转换。然而,这对我不起作用。我刚刚收到“no_transition”。这是我的示例代码:
执行时,它会产生以下输出:
进入:State1
在事件 N12_GLOBAL__N_110some_eventE 上没有从状态 0 转换
离开:State1
进入:State2
我希望在“some_event”上发生从“State1”到“State2”的转换,但这显然不会发生。
我一定错过了什么,但我无法意识到它是什么。
c++ - 我可以避免所有这些倍数 try/catch
我有许多 boost::any 的向量在这个向量中我需要对 std::vector 和 IContainer 类型的元素执行一些操作
所以我做了以下功能(imo很丑)但谁工作正常
米是const std::vector<boost::any>&
我尝试添加许多“ try{}try{}catch{}
”但它不起作用
你有比我做的更好的解决方案吗
编辑
我尝试过 James Kanze、user1131467 和 Praetorian 的解决方案
所以这3个工作得很好,但是当我计算出执行时间时,user1131467的答案比另一个快一点。我现在必须找到一种解决方案来将每种类型存储在地图中,以避免所有这些 if/else
我还将看看 boost::variant