有什么先天优势boost::any
and boost::any_cast
提供超过使用void*
and dynamic_cast
?
优点是boost::any
比类型安全得多void*
.
E.g.
int i = 5;
void* p = &i;
static_cast<double*>(p); //Compiler doesn't complain. Undefined Behavior.
boost::any a;
a = i;
boost::any_cast<double>(a); //throws, which is good
至于你的评论,你不能dynamic_cast
from a void*
。你可以dynamic_cast
仅来自对至少具有一个虚函数(也称为多态类型)的类类型的指针和引用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)