How to print boost::any to a stream?

前端 未结 9 1312
悲哀的现实
悲哀的现实 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:56

    You could use boost::spirit::hold_any instead. It's defined here:

    #include 
    

    and is fully compatible with boost::any. This class has two differences if compared to boost::any:

    • it utilizes the small object optimization idiom and a couple of other optimization tricks, making spirit::hold_any smaller and faster than boost::any
    • it has the streaming operators (operator<<() and operator>>()) defined, allowing to input and output a spirit::hold_any seemlessly.

    The only limitation is that you can't input into an empty spirit::hold_any, but it needs to be holding a (possibly default constructed) instance of the type which is expected from the input.

提交回复
热议问题