ostream chaining, output order

前端 未结 5 1295
礼貌的吻别
礼貌的吻别 2020-11-30 11:38

I have a function that takes an ostream reference as an argument, writes some data to the stream, and then returns a reference to that same stream, like so:

5条回答
  •  被撕碎了的回忆
    2020-11-30 12:04

    In your statement std::cout << "Hello, world!" << print( std::cout ) << std::endl it's undefined whether std::cout << "Hello, world!" happens before or after print( std::cout ). That's why the order may not be what you expect.

    The hex value comes from the fact that you're also doing std::cout << std::cout (print returns std::cout which is fed into the << chain). The right hand std::cout is converted to a void * and that's printed to the output.

提交回复
热议问题