我注意到我们打印指针的方式存在差异。默认情况下,gcc 会在指针的十六进制输出中添加 0x 前缀,而 Microsoft 的编译器不会这样做。 showbase/noshowbase 不会影响它们中的任何一个。
#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;
}
我假设这是实现定义的行为而不是错误,但是有没有办法阻止任何编译器在 0x 之前添加?我们已经在我们自己的前面添加了 0x,但在 gcc 中它像 0x0xABCD 一样出现。
我确定我可以做类似 ifdef __GNUC___ ....
的事情,但我想知道我是否遗漏了更明显的东西。谢谢
最佳答案
我建议您转换为 intptr
并将输入视为普通整数。
然后,您的 I/O 操纵器应该可以工作了。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
void * n = (void *)1;
cout << noshowbase << hex << reinterpret_cast<intptr_t>(n) << dec << endl;
n = (void *)10;
cout << noshowbase << hex << reinterpret_cast<intptr_t>(n) << dec << endl;
n = (void *)0;
cout << noshowbase << hex << reinterpret_cast<intptr_t>(n) << dec << endl;
}
// 1
// a
// 0
( live demo )
起初我对您的问题感到有些惊讶(更具体地说,是这些实现的行为),但我越想越觉得它有道理。指针不是数字*,在如何呈现它们方面确实没有比实现更好的权威。
* 我是认真的!虽然,有趣的是,出于实现/历史原因,标准在这种情况下调用 const void*
“数字”,使用 num_put
区域设置内容进行格式化输出,最终推迟到 [facet.num.put.virtuals]
中的 printf
。该标准规定应使用格式说明符 %p
但是,由于 %p
的结果是实现定义的,因此您实际上可以使用当前代码获得任何东西.
关于C++ 打印指针不确认 showbase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44122966/