Switches Gone Wild

closed account (3hM2Nwbp)
I was reading through the source of boost::asio just a few moments ago when I stumbled across a piece of code that almost made my eye-balls pop out...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    void operator()(const boost::system::error_code& ec,
        std::size_t bytes_transferred, int start = 0)
    {
      std::size_t n = 0;
      switch (start)
      {
        case 1:
        n = this->check_for_completion(ec, total_transferred_);
        for (;;)
        {
          stream_.async_read_some(boost::asio::buffer(
                buffer_ + total_transferred_, n), *this);
          return; default:
          total_transferred_ += bytes_transferred;
          if ((!ec && bytes_transferred == 0)
              || (n = this->check_for_completion(ec, total_transferred_)) == 0
              || total_transferred_ == boost::asio::buffer_size(buffer_))
            break;
        }

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


So I made up a little test to compile with MSVC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int four = 4;
    switch(four)
    {
        case 1:
            for(;;)
            {
        default:
            return 0xBADC0DE;
            }
    }
    return 0;
}


It...just......horrible. Is this behavior standard?
It is valid and does what is needed -- though it is a bit difficult to follow.

The basic premise is best known because of Duff's Device.
http://en.wikipedia.org/wiki/Duff%27s_device
http://www.faqs.org/docs/jargon/D/Duff%27s-device.html
closed account (3hM2Nwbp)
Hmm...I wonder if this type of thing is optimized by the compiler without making a mess of a code section.

I think I'll compare the disassembly of two snippits and see.

Thanks Duoas
Last edited on
I don't think that code was meant to be read by anyone :s
Topic archived. No new replies allowed.