C++ printing pointer doesn't acknowledge showbase

Viewed 136

I've noticed a discrepancy in the way we print pointers. gcc by default is adding 0x prefix to hex output of pointer, and Microsoft's compiler doesn't do that. showbase/noshowbase doesn't affect either of them.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
void * n = (void *)1;
cout << noshowbase << hex << n << dec << endl;
// output (g++ (GCC) 4.7.2, 5.4.0): 0x1
// output (VS 2010, 2013): 00000001

n = (void *)10;
cout << noshowbase << hex << n << dec << endl;
// output (g++ (GCC) 4.7.2, 5.4.0): 0xa
// output (VS 2010, 2013): 0000000A

n = (void *)0;
cout << noshowbase << hex << n << dec << endl;
// output (g++ (GCC) 4.7.2, 5.4.0): 0
// output (VS 2010, 2013): 00000000

return 0;
}

I assume this is implementation defined behavior and not a bug, but is there a way to stop any compiler from prepending the 0x? We are already prepending the 0x on our own but in gcc it comes out like 0x0xABCD.

I'm sure I could do something like ifdef __GNUC___ .... but I wonder if I'm missing something more obvious. Thanks

1 Answers
Related