【发布时间】:2014-04-07 22:11:01
【问题描述】:
以下测试程序根据我使用的是 libc++ 还是 libstdc++ 返回不同的结果。
#include <sstream>
#include <iostream>
int main()
{
int a = 0;
void* optr = &a;
void* iptr;
std::stringstream ss;
ss << optr;
std::cout << ss.str() << '\n';
ss >> iptr;
std::cout << iptr << '\n';
return 0;
}
我在 OSX 10.9.2 上使用来自 Xcode 5 的以下版本的 clang
$ xcrun clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
这是使用 libstdc++ 和 libc++ 构建时的测试输出
$ xcrun clang++ test.cpp <-- libstdc++ version
$ ./a.out
0x7fff5ec723e8
0x7fff5ec723e8
$ xcrun clang++ test.cpp -stdlib=libc++ <-- libc++ version
$ ./a.out
0x7fff5205125c
0x7fff5
这是 stringstream 的 libc++ 实现中的错误吗? void* 与 stringstream 的这种用法是有效的 C++ 吗?
谢谢!
【问题讨论】:
-
[locale.num.get] 似乎是指定这一点的标准部分。似乎说
ss >> iptr的行为应该像sscanf和%p但AFAICS 它实际上并没有指定这一点。 “第 3 阶段”描述似乎涵盖了整数和浮点类型,但它忽略了void *。sscanf(ss.str().c_str(), "%p", &iptr);有效吗? -
@MattMcNabb:我刚刚用 clang-503.0.38(基于 LLVM 3.4svn)进行了测试,
sscanf(ss.str().c_str(), "%p", &iptr);确实对我有用。
标签: c++ llvm stringstream libc++