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

      1. <small id='ZQb6x'></small><noframes id='ZQb6x'>

      2. <legend id='ZQb6x'><style id='ZQb6x'><dir id='ZQb6x'><q id='ZQb6x'></q></dir></style></legend>
      3. <tfoot id='ZQb6x'></tfoot>

          <bdo id='ZQb6x'></bdo><ul id='ZQb6x'></ul>

        无论如何将以下内容编写为 C++ 宏?

        时间:2023-09-18
        <i id='O84uK'><tr id='O84uK'><dt id='O84uK'><q id='O84uK'><span id='O84uK'><b id='O84uK'><form id='O84uK'><ins id='O84uK'></ins><ul id='O84uK'></ul><sub id='O84uK'></sub></form><legend id='O84uK'></legend><bdo id='O84uK'><pre id='O84uK'><center id='O84uK'></center></pre></bdo></b><th id='O84uK'></th></span></q></dt></tr></i><div id='O84uK'><tfoot id='O84uK'></tfoot><dl id='O84uK'><fieldset id='O84uK'></fieldset></dl></div>
          <bdo id='O84uK'></bdo><ul id='O84uK'></ul>

          1. <tfoot id='O84uK'></tfoot>

                <tbody id='O84uK'></tbody>

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

                  <legend id='O84uK'><style id='O84uK'><dir id='O84uK'><q id='O84uK'></q></dir></style></legend>
                  本文介绍了无论如何将以下内容编写为 C++ 宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  my_macro << 1 << "hello world" << blah->getValue() << std::endl;
                  

                  应该扩展为:

                  std::ostringstream oss;
                  oss << 1 << "hello world" << blah->getValue() << std::endl;
                  ThreadSafeLogging(oss.str());
                  

                  推荐答案

                  #define my_macro my_stream()
                  class my_stream: public std::ostringstream  {
                  public:
                      my_stream() {}
                      ~my_stream() {
                          ThreadSafeLogging(this->str());
                      }
                  };
                  int main() {
                      my_macro << 1 << "hello world" << std::endl;
                  }
                  

                  创建了一个 my_stream 类型的临时文件,它是 ostringstream 的子类.对该临时文件的所有操作都像在 ostringstream 上一样工作.

                  A temporary of type my_stream is created, which is a subclass of ostringstream. All operations to that temporary work as they would on an ostringstream.

                  当语句结束时(即在 main() 中整个打印操作的分号之后),临时对象超出范围并被销毁.my_stream 析构函数使用先前收集"的数据调用 ThreadSafeLogging.

                  When the statement ends (ie. right after the semicolon on the whole printing operation in main()), the temporary object goes out of scope and is destroyed. The my_stream destructor calls ThreadSafeLogging with the data "collected" previously.

                  已测试 (g++).

                  感谢/感谢 dingo 指出如何简化整个事情,所以我不需要重载的 operator<<.太糟糕了,无法分享点赞.

                  Thanks/credits to dingo for pointing out how to simplify the whole thing, so I don't need the overloaded operator<<. Too bad upvotes can't be shared.

                  这篇关于无论如何将以下内容编写为 C++ 宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何轻松使 std::cout 线程安全? 下一篇:现成的 C++ 十六进制转储代码

                  相关文章

                  最新文章

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

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

                    2. <legend id='f0WQn'><style id='f0WQn'><dir id='f0WQn'><q id='f0WQn'></q></dir></style></legend>