[英]Difference between boost::any and boost::variant when we use cout<<
I have run the following piece of code successfully using boost::variant<string, float>
. 我使用
boost::variant<string, float>
成功运行了以下代码。 However, when I tried to use boost::any
instead, I face mismatch operand error at cout<<
instruction, the piece of code is : 但是,当我尝试使用
boost::any
,我在cout<<
指令中遇到不匹配的操作数错误, cout<<
代码是:
for( vector<vector<vector<boost::any>>>::const_iterator i = masterList.begin(); i != masterList.end(); ++i)
{
for( vector<vector<boost::any>>::const_iterator j = i->begin(); j != i->end(); ++j)
{
for( vector<boost::any>::const_iterator k = j->begin(); k != j->end(); ++k)
{
cout<<*k<<' ';
}
}
}
Boost.Any offers full type erasure, all characteristics (such as streaming to a std::ostream
) of the underlying type are erased. Boost.Any提供完整类型擦除,删除基础类型的所有特征(例如流式传输到
std::ostream
)。 The only way to get back the type is by using the any_cast
functions. 获取类型的唯一方法是使用
any_cast
函数。
If you want partial type erasure, look at the Boost.TypeErasure library of Steven Watanabe. 如果你想要部分类型擦除,请查看Steven Watanabe的Boost.TypeErasure库。 Note that TypeErasure is an official Boost library since Boost 1.54.
请注意,TypeErasure是自Boost 1.54以来的官方Boost库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.