网上有很多boost::any的源码解析,可以看看一位大牛的博客:http://blog.csdn.net/pongba/archive/2004/08/24/82811.aspx
对其中已经解析过的部分我就不再啰嗦,我只做两点额外的并觉得重要的补充:
(1) 在赋值操作符中使用了Copy-Swap手法,从而达到强异常安全的保证。 (关于此手法的详解,可以参考More Effective C++里面关于异常安全的讲解)
(2) 在any_cast(any & operand)方法中使用了去引用的类型萃取方法 ,此方法技巧性很强,在《Modern C++ Development》中有讲解。
为何要这么做,举个例子:
int i = 0;
any a = i;
int j = any_cast(a); // 这里没问题
int k = any_cast(a); // 如果没有去引用手法,这里就会出错,原因如下解析:
any_cast源码:
template
ValueType * any_cast(any * operand) // any_cast 1
{
return operand && operand->type() == typeid(ValueType)
? &static_cast<:holder> *>(operand->content)->held
: 0;
}
template
inline const ValueType * any_cast(const any * operand) // any_cast 2
{
return any_cast(const_cast(operand));
}
template
ValueType any_cast(any & operand) // any_cast 3
{
typedef BOOST_DEDUCED_TYPENAME remove_reference::type nonref; // 句1 ,用于去引用
...
nonref * result = any_cast<nonref >(&operand); // 句2
if(!result)
boost::throw_exception(bad_any_cast());
return *result;
}
如果any_cast 3中没有句1,那句2将变成:
ValueType* result = any_cast(&operand);
此时,对于“any_cast(a)”这样的语句,将被编译成:
int&* reslut = any_cast(&operand); // 对于int&*这样的语法,编译时就会出错
假设编译通过,此句将调用any_cast 2:
any_cast(const_cast(operand));
接着调用any_cast 1,有两问题也就跟着出来了:
(1) operand->type() == typeid(ValueType);
这句话其实没问题,typeid(int)跟typeid(int&)是一样的
(2)static_cast<:holder> *>(operand->content)
这里any::holder跟any::holder是属于不同的类型,但编译可以通过,执行时就会发生crash.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7364811/viewspace-664099/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7364811/viewspace-664099/