How to print boost::any to a stream?

前端 未结 9 1304
悲哀的现实
悲哀的现实 2020-12-03 07:48

I have a Map std::map, which comes from the boost::program_options package. Now I would like to print the content o

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 07:54

    Rather than re-writing my class to use boost::spirit::hold_any, I created a way to stream boost::any, similar to what manifest suggested, but just in one place.

    ostream& operator<<(ostream& _os, const boost::any& _any)
    {
      // only define simple type conversions
      if (_any.type() == typeid(int))
        _os << boost::any_cast(_any);
    
       /*any other types you use...*/
    }
    

    Rather cumbersome, but it allows me to stream a boost::any variable anywhere in my code.

    How about being able to construct a boost::spirit::hold_any from a boost:any?

提交回复
热议问题