网上有很多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
int k = any_cast
any_cast源码:
template
ValueType * any_cast(any * operand) // any_cast 1
{
return operand && operand->type() == typeid(ValueType)
? &static_cast
: 0;
}
template
inline const ValueType * any_cast(const any * operand) // any_cast 2
{
return any_cast
}
template
ValueType any_cast(any & operand) // any_cast 3
{
typedef BOOST_DEDUCED_TYPENAME remove_reference
...
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
此时,对于“any_cast
int&* reslut = any_cast
假设编译通过,此句将调用any_cast 2:
any_cast
接着调用any_cast 1,有两问题也就跟着出来了:
(1) operand->type() == typeid(ValueType);
这句话其实没问题,typeid(int)跟typeid(int&)是一样的
(2)static_cast
这里any::holder
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7364811/viewspace-664099/ ,如需转载,请注明出处,否则将追究法律责任。