Order of execution in operator <<

后端 未结 3 918
梦谈多话
梦谈多话 2020-11-29 12:18

I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below

    A1B2

While I can

3条回答
  •  猫巷女王i
    2020-11-29 12:58

    The order of execution of << is well defined but the order of evaluation of sub-expressions is not defined in C++. This article and the C code example illustrates the problem you mentioned.

    BA12 and AB12 are both correct. In the following code:

    std::cout<< b->fooA() << b->fooB()
    

    1 will appear before 2 but A could appear before or after B since the compiler does not promise whether it will evaluate fooA or fooB first.

提交回复
热议问题