简体   繁体   中英

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> . However, when I tried to use boost::any instead, I face mismatch operand error at cout<< instruction, the piece of code is :

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. The only way to get back the type is by using the any_cast functions.

If you want partial type erasure, look at the Boost.TypeErasure library of Steven Watanabe. Note that TypeErasure is an official Boost library since Boost 1.54.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM