繁体   English   中英

获取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::any_cast

如果您愿意使用不受支持的内部hack,则当前版本的头文件具有未记录且不受支持的函数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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM