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 ?
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.
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 ).
Source: https://ru.stackoverflow.com/questions/511784/
All Articles
flipmethod? That would narrow the question. - KromsterGCC 4.9.2- anton_s