use*_*777 7 c++ boost boost-any
我正在使用一个外部库,它有一个接受空白的方法*
我希望这个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;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这是不可能的;boost::any_cast如果类型与包含的类型不同,将拒绝强制转换。
如果您愿意使用不受支持的内部黑客,当前版本的标头有一个未记录且不受支持的函数boost::unsafe_any_cast,该函数(顾名思义)绕过了执行的类型检查boost::any_cast:
boost::any any_value(value);
void *content = boost::unsafe_any_cast<void *>(&any_value);
Run Code Online (Sandbox Code Playgroud)
标题有这样的说法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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1635 次 |
| 最近记录: |