|
|
|
|
|
by xeyownt
1905 days ago
|
|
That construction is ok. But usually you want formatted output, let's say align to byte, and pad with zeroes. I've seen oftentimes (and did myself): std::cout << std::setfill('0') << std::setw(2) << std::hex << my_int << std::dec << std::endl;
This appears to work until someone change the alignment to left somewhere in the code. Hence the correct code is: // C++ type-safe equiv to printf("%-02x",my_int) - it's called progress
std::cout << std::right << std::setfill('0') << std::setw(2) << std::hex << my_int << std::dec << std::endl;
Also, is it relevant to keep the final 'dec' when we assume we can't assert the ios left/right state, so why could we assert the hex/dec state? Or maybe was it a bug to change alignment to left, and not restore it to right afterwards? Or maybe should you restore ios state in some centrol place, and never pass your iostream to some external libs? Discussions and wars ahead. Note that the bug above is very nasty because it will change say "02" into "20", which looks perfectly valid.Note: I just noticed that in C++20, there is new formatted string proposal. You can't stop progress, but neither can you speed it up it seems. Note2: the 'std::' spam is yet another indication that C++ is lacking common sense and utterly broken. |
|