获取 void* 指向 boost::any 内容的指针
Obtain void* pointer to content of boost::any
我正在使用一个外部库,该库具有接受 void
* 的方法*我希望这个 void* 指向包含在 boost::any 对象中的对象。
是否可以获取 boost::any 对象的内容的地址?
我正在尝试玩myAny.content,但到目前为止没有运气!我希望dynamic_cast或unsafe_any_cast的某种组合能给我所需要的。
谢谢!
您可以使用
boost::any_cast
获取指向基础类型的指针(前提是您在编译时知道它)。
boost::any any_i(5);
int* pi = boost::any_cast<int>(&any_i);
*pi = 6;
void* vpi = pi;
不幸的是,这是不可能的; 如果类型与包含的类型不同,boost::any_cast
将拒绝强制转换。
如果您愿意使用不受支持的内部黑客,则当前版本的标头具有未记录且不受支持的函数boost::unsafe_any_cast
,该函数(顾名思义)绕过了boost::any_cast
执行的类型检查:
boost::any any_value(value);
void *content = boost::unsafe_any_cast<void *>(&any_value);
标题对unsafe_any_cast
有这样说:
// Note: The "unsafe" versions of any_cast are not part of the
// public interface and may be removed at any time. They are
// required where we know what type is stored in the any and can't
// use typeid() comparison, e.g., when our types may travel across
// different shared libraries.
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 将特定指针强制转换为 std::any 指针
- 获取 void* 指向 boost::any 内容的指针
- 获取指向 boost::any::operator= 的指针
- typedef 以下类型:指向具有成员函数 F 的成员函数 Fof "any"类的指针
- 从boost::any中恢复函数指针
- 从boost::any的指针调用可变进模板