fork download
  1. #include <ostream>
  2. #include <iostream>
  3. #include <locale>
  4.  
  5. using namespace std;
  6.  
  7. inline int geti() {
  8. static int i = ios_base::xalloc();
  9. return i;
  10. }
  11.  
  12. // rename it to something better
  13. struct my_num_put : num_put<char> {
  14. static const std::ios_base::fmtflags reqFlags
  15. = (std::ios_base::showbase | std::ios_base::hex);
  16.  
  17. iter_type
  18. do_put(iter_type s, ios_base& f, char_type fill, long v) const {
  19. if (v == 0 && ((f.flags() & reqFlags) == reqFlags)) {
  20. *(s++) = '0';
  21. *(s++) = 'x';
  22. }
  23. return num_put<char>::do_put(s, f, fill, v);
  24. }
  25.  
  26. iter_type
  27. do_put(iter_type s, ios_base& f, char_type fill, unsigned long v) const {
  28. if (v == 0 && ((f.flags() & reqFlags) == reqFlags)) {
  29. *(s++) = '0';
  30. *(s++) = 'x';
  31. }
  32. return num_put<char>::do_put(s, f, fill, v);
  33. }
  34. };
  35.  
  36. int main() {
  37. // wrong
  38. std::cout << std::showbase << std::hex << 0 << ' ' << 11 << std::endl;
  39.  
  40. // fixed by workaround
  41. std::cout.imbue(std::locale(std::locale(), new my_num_put));
  42. std::cout << std::showbase << std::hex << 0 << ' ' << 11 << std::endl;
  43.  
  44. // sanity check
  45. std::cout.unsetf(my_num_put::reqFlags);
  46. std::cout << 0 << ' ' << 11 << std::endl;
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
0 0xb
0x0 0xb
0 11