std::bitset<8> b(1); std::cout << b << std::endl << b.flip(1) << std::endl << b; 

Why console output after these instructions

 00000011 00000011 00000011 

, but not

 00000001 00000011 00000011 

?

  • I have different: 00000001, 00000011. - AivanF.
  • Can you check the problem in the output or in the flip method? That would narrow the question. - Kromster
  • Oh, and now a different look at the question. We thought, the flip did not work for you, but no, just the opposite. Apparently, in your compiler, this code was converted to such a view that first the calculations were performed, and then the output was just sent. By the way, the name of the compiler in the studio, please. - AivanF.
  • @AivanF. GCC 4.9.2 - anton_s
  • One gets the feeling that there are simply no sequence points, as a result, the order of calculating the arguments is not explicitly stated. - StateItPrimitive

2 answers 2

In C ++, the order of calculating the arguments of a function is not defined ( Standard C ++ section 5.2.2 / 8). You can think about the construction (omit << std::endl << b; for short)

 std::cout << b << std::endl << b.flip(1); 

how about:

 std::operator<<(std::operator<<(std::cout, b), b.flip(1)); 

Obviously, based on the standard, nothing prevents the compiler from first b.flip(1) . Accordingly, an undefined behavior arises (since the nearest point of the sequence is found when the function is called, after calculating its arguments).

PS Here is a good article on the topic of points.

  • The concept of 'point of sequence' does not exist since 'c ++ 11'. - αλεχολυτ
  • @alexolut But, in fact, the behavior of the compiler assigned to this concept remains the same (the concept was abolished, but in fact nothing has changed, well, or at least I don't know what exactly has changed: D). All operations are still divided into guaranteeing the sequence of performing certain actions and not guaranteeing. Those. The notion of a “point of succession” was replaced by two: “the operation is sequential” or “the operation is not consecutive” (ala rename ?). - StateItPrimitive
  • @StateItPrimitive Behavior remains, but the description of this behavior began to rely on the mathematical concept of "attitude." In short, it became better, simpler and more unambiguous. If you haven't read it yet, here is the answer to enSO - αλεχολυτ

In online compilers of different versions of pluses (like ideone and C ++ Shell ), the output is the same as yours. It seems that this is not an exception, but the norm. Perhaps this is spelled out in the specification. In general, the use of several different values ​​of one variable in one line threatens with uncertainty ( cppreference.com ).