4

I guess this is a question to understand the boost source code(boost::asio::detail::write_op). The code snippet from boost source code repo is as follows.

    void operator()(const boost::system::error_code& ec,
        std::size_t bytes_transferred, int start = 0)
    {
      typename boost::asio::detail::dependent_type<Elem,
          std::array<boost::asio::const_buffer, 2> >::type bufs = {{
        boost::asio::const_buffer(buffers_[0]),
        boost::asio::const_buffer(buffers_[1]) }};
      std::size_t buffer_size0 = boost::asio::buffer_size(bufs[0]);
      std::size_t buffer_size1 = boost::asio::buffer_size(bufs[1]);
      std::size_t n = 0;
      switch (start_ = start)
      {
        case 1:
        n = this->check_for_completion(ec, total_transferred_);
        for (;;)
        {
          bufs[0] = boost::asio::buffer(bufs[0] + total_transferred_, n);
          bufs[1] = boost::asio::buffer(
              bufs[1] + (total_transferred_ < buffer_size0
                ? 0 : total_transferred_ - buffer_size0),
              n - boost::asio::buffer_size(bufs[0]));
          stream_.async_write_some(bufs, BOOST_ASIO_MOVE_CAST(write_op)(*this));
          return; default:
          total_transferred_ += bytes_transferred;
          if ((!ec && bytes_transferred == 0)
              || (n = this->check_for_completion(ec, total_transferred_)) == 0
              || total_transferred_ == buffer_size0 + buffer_size1)
            break;
        }

        handler_(ec, static_cast<const std::size_t&>(total_transferred_));
      }
    }

In this code, I couldn't understand the purpose of default case being placed inside the for loop. Why would one implement a case statement this way ? I am just curious.

Any explanation would be a great help.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Gavi
  • 73
  • 4
  • 1
    Wow. I didn't think [this](https://godbolt.org/z/D4DcYr) was possible. This is amazing. – Timo Mar 04 '20 at 21:23
  • 3
    [Duff's device](https://stackoverflow.com/questions/514118/how-does-duffs-device-work)? – G.M. Mar 04 '20 at 21:59
  • Looks like somebody's attempt at a clever way to avoid code duplication, although it looks like that could be done more conventionally with `if` statements. – 1201ProgramAlarm Mar 04 '20 at 22:12

0 Answers0