<i id='ocLf7'><tr id='ocLf7'><dt id='ocLf7'><q id='ocLf7'><span id='ocLf7'><b id='ocLf7'><form id='ocLf7'><ins id='ocLf7'></ins><ul id='ocLf7'></ul><sub id='ocLf7'></sub></form><legend id='ocLf7'></legend><bdo id='ocLf7'><pre id='ocLf7'><center id='ocLf7'></center></pre></bdo></b><th id='ocLf7'></th></span></q></dt></tr></i><div id='ocLf7'><tfoot id='ocLf7'></tfoot><dl id='ocLf7'><fieldset id='ocLf7'></fieldset></dl></div>

    <small id='ocLf7'></small><noframes id='ocLf7'>

    <legend id='ocLf7'><style id='ocLf7'><dir id='ocLf7'><q id='ocLf7'></q></dir></style></legend>
      <bdo id='ocLf7'></bdo><ul id='ocLf7'></ul>

  • <tfoot id='ocLf7'></tfoot>

        如何将 boost::any 打印到流?

        时间:2023-07-20

            <tbody id='hK3Pz'></tbody>

          <i id='hK3Pz'><tr id='hK3Pz'><dt id='hK3Pz'><q id='hK3Pz'><span id='hK3Pz'><b id='hK3Pz'><form id='hK3Pz'><ins id='hK3Pz'></ins><ul id='hK3Pz'></ul><sub id='hK3Pz'></sub></form><legend id='hK3Pz'></legend><bdo id='hK3Pz'><pre id='hK3Pz'><center id='hK3Pz'></center></pre></bdo></b><th id='hK3Pz'></th></span></q></dt></tr></i><div id='hK3Pz'><tfoot id='hK3Pz'></tfoot><dl id='hK3Pz'><fieldset id='hK3Pz'></fieldset></dl></div>

        1. <legend id='hK3Pz'><style id='hK3Pz'><dir id='hK3Pz'><q id='hK3Pz'></q></dir></style></legend>
        2. <small id='hK3Pz'></small><noframes id='hK3Pz'>

          • <bdo id='hK3Pz'></bdo><ul id='hK3Pz'></ul>

                <tfoot id='hK3Pz'></tfoot>
                  本文介绍了如何将 boost::any 打印到流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Map std::map<std::string, boost::any>,它来自 boost::program_options 包.现在我想打印该地图的内容:

                  I have a Map std::map<std::string, boost::any>, which comes from the boost::program_options package. Now I would like to print the content of that map:

                  for(po::variables_map::const_iterator it = vm.begin(); it != vm.end(); ++it) {
                    std::cerr << it->first << ": " << it->second << std::endl;
                  }
                  

                  不幸的是,这是不可能的,因为 boost::any 没有定义 operator<<.

                  Unfortunately, that is not possible because boost::any doesn't have an operator<< defined.

                  打印该地图的最简单方法是什么?

                  What is the easiest way to print that map?

                  我可以为任何自动尝试将每个 any 转换为 int、double、string 等的输出操作符定义我自己的输出操作符,每次忽略错误并尝试转换直到转换成功,我可以按指定类型打印.

                  I could define my own output operator for any that automatically tries to cast each any to an int, then double, then string, etc., each time ignoring errors and trying to cast until the cast is successful and I can print as the specified type.

                  但是Boost中应该有更简单的方法吗?我需要类似反向lexical_cast...

                  But there should be an easier method in Boost? I'd need something like a reverse lexical_cast...

                  推荐答案

                  您可以使用 boost::spirit::hold_any 代替.它在这里定义:

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

                  #include <boost/spirit/home/support/detail/hold_any.hpp>
                  

                  并且与 boost::any 完全兼容.与 boost::any 相比,这个类有两个不同:

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

                  • 它利用了小对象优化习惯用法和其他一些优化技巧,使 spirit::hold_anyboost::any 更小、更快
                  • 它定义了流操作符(operator<<()operator>>()),允许输入和输出 精神::hold_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.

                  唯一的限制是你不能输入一个空的spirit::hold_any,但它需要持有一个(可能是默认构造的)输入类型的实例.

                  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.

                  这篇关于如何将 boost::any 打印到流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:tr1::reference_wrapper 有什么用? 下一篇:Boost.Asio 作为仅标头

                  相关文章

                  <i id='LQoGK'><tr id='LQoGK'><dt id='LQoGK'><q id='LQoGK'><span id='LQoGK'><b id='LQoGK'><form id='LQoGK'><ins id='LQoGK'></ins><ul id='LQoGK'></ul><sub id='LQoGK'></sub></form><legend id='LQoGK'></legend><bdo id='LQoGK'><pre id='LQoGK'><center id='LQoGK'></center></pre></bdo></b><th id='LQoGK'></th></span></q></dt></tr></i><div id='LQoGK'><tfoot id='LQoGK'></tfoot><dl id='LQoGK'><fieldset id='LQoGK'></fieldset></dl></div>
                    <bdo id='LQoGK'></bdo><ul id='LQoGK'></ul>
                  <tfoot id='LQoGK'></tfoot>

                      <legend id='LQoGK'><style id='LQoGK'><dir id='LQoGK'><q id='LQoGK'></q></dir></style></legend>
                    1. <small id='LQoGK'></small><noframes id='LQoGK'>