C++ 使用libc++;和libstdc++;
下面的测试程序根据我使用的是libc++还是libstdc++返回不同的结果C++ 使用libc++;和libstdc++;,c++,llvm,stringstream,libc++,C++,Llvm,Stringstream,Libc++,下面的测试程序根据我使用的是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() <<
#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;
}
以下是使用libstdc++和libc构建时的测试输出++
$ xcrun clang++ test.cpp <-- libstdc++ version
$ ./a.out
0x7fff5ec723e8
0x7fff5ec723e8
$ xcrun clang++ test.cpp -stdlib=libc++ <-- libc++ version
$ ./a.out
0x7fff5205125c
0x7fff5
$xcrun clang++test.cpp是!这是libc++中的一个bug,可能是在\uuu sscanf\l
的实现中(有些scanf
看起来很像,应该考虑到区域设置)。libstdc++的实现要简单得多
// libc++
template <class _CharT, class _InputIterator>
_InputIterator
num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
ios_base& __iob,
ios_base::iostate& __err,
void*& __v) const
{
// Stage 1
int __base = 16;
// Stage 2
char_type __atoms[26];
char_type __thousands_sep = 0;
string __grouping;
use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
__num_get_base::__src + 26, __atoms);
string __buf;
__buf.resize(__buf.capacity());
char* __a = &__buf[0];
char* __a_end = __a;
unsigned __g[__num_get_base::__num_get_buf_sz];
unsigned* __g_end = __g;
unsigned __dc = 0;
for (; __b != __e; ++__b)
{
if (__a_end == __a + __buf.size())
{
size_t __tmp = __buf.size();
__buf.resize(2*__buf.size());
__buf.resize(__buf.capacity());
__a = &__buf[0];
__a_end = __a + __tmp;
}
if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
__thousands_sep, __grouping,
__g, __g_end, __atoms))
break;
}
// Stage 3
__a[sizeof(__a)-1] = 0;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
#else
if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
#endif
__err = ios_base::failbit;
// EOF checked
if (__b == __e)
__err |= ios_base::eofbit;
return __b;
}
//libc++
模板
_输入计数器
num_get::do_get(iter_类型uu b,iter_类型u e,
ios_基地和iob,
ios_base::iostate&\uu err,
无效*&常数
{
//第一阶段
int _u基数=16;
//第二阶段
char_型__原子[26];
字符类型为0;
字符串分组;
使用面(uuuiob.getloc())。加宽(uunum\uget\ubase::uuusrc,
__num_get_base::__src+26,__原子);
字符串_; buf;
__buf.resize(uu buf.capacity());
char*uuu a=&uuuu buf[0];
字符*\uuuu a\u end=\uuuuu a;
未签名的uu g[u num_get_base::u num_get_buf_sz];
无符号*\uuu g\u end=\uuu g;
无符号uu dc=0;
对于(;uuu b!=uu e;++uu b)
{
如果(\uuuu a\u end==\uuuuu a+\uuu buf.size())
{
size_t_utmp=u buf.size();
__调整大小(2*uu buf.size());
__buf.resize(uu buf.capacity());
__a=&uuubuf[0];
__a_端=u a+u tmp;
}
如果(此->阶段2\u int\u循环(*uuuuuu b、\uuuu基、\uuuu a、\uuuu a\u端、\uuuu dc、,
__千年9月,分组,
__g、 _uug_end,uu原子)
打破
}
//第三阶段
__a[sizeof(_a)-1]=0;
#ifdef\u LIBCPP\u语言环境\u语言扩展
如果(sscanf_l(__a,_LIBCPP_GET_C_LOCALE,“%p,&_v)!=1)
#否则
如果(uuu sscanf_ul(uuu a,uuu cloc(),%p,&uuu v)!=1)
#恩迪夫
__err=ios_base::failbit;
//已检查EOF
如果(uuu b==uuu e)
__err |=ios_base::eofbit;
返回b;
}
对
// libstdc++
template<typename _CharT, typename _InIter>
_InIter
num_get<_CharT, _InIter>::
do_get(iter_type __beg, iter_type __end, ios_base& __io,
ios_base::iostate& __err, void*& __v) const
{
// Prepare for hex formatted input.
typedef ios_base::fmtflags fmtflags;
const fmtflags __fmt = __io.flags();
__io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
typedef __gnu_cxx::__conditional_type<(sizeof(void*)
<= sizeof(unsigned long)),
unsigned long, unsigned long long>::__type _UIntPtrType;
_UIntPtrType __ul;
__beg = _M_extract_int(__beg, __end, __io, __err, __ul);
// Reset from hex formatted input.
__io.flags(__fmt);
__v = reinterpret_cast<void*>(__ul);
return __beg;
}
//libstdc++
模板
_始作俑者
num_get::
得到(iter类型beg、iter类型end、ios基础和io、,
ios_base::iostate&__err,void*&_v)const
{
//准备十六进制格式的输入。
typedef ios_base::fmtflags fmtflags;
常量fmtflags uu fmt=uuu io.flags();
__io.flags((uu fmt&~ios_base::basefield)| ios_base::hex);
typedef\uu gnu\u cxx::\uu conditional\u type是的!这是libc++中的一个bug,可能是在\uu sscanf\l
的实现中(一些scanf
看起来很像,应该考虑到区域设置)。libstdc++的实现要简单得多
// libc++
template <class _CharT, class _InputIterator>
_InputIterator
num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
ios_base& __iob,
ios_base::iostate& __err,
void*& __v) const
{
// Stage 1
int __base = 16;
// Stage 2
char_type __atoms[26];
char_type __thousands_sep = 0;
string __grouping;
use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
__num_get_base::__src + 26, __atoms);
string __buf;
__buf.resize(__buf.capacity());
char* __a = &__buf[0];
char* __a_end = __a;
unsigned __g[__num_get_base::__num_get_buf_sz];
unsigned* __g_end = __g;
unsigned __dc = 0;
for (; __b != __e; ++__b)
{
if (__a_end == __a + __buf.size())
{
size_t __tmp = __buf.size();
__buf.resize(2*__buf.size());
__buf.resize(__buf.capacity());
__a = &__buf[0];
__a_end = __a + __tmp;
}
if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
__thousands_sep, __grouping,
__g, __g_end, __atoms))
break;
}
// Stage 3
__a[sizeof(__a)-1] = 0;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
#else
if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
#endif
__err = ios_base::failbit;
// EOF checked
if (__b == __e)
__err |= ios_base::eofbit;
return __b;
}
//libc++
模板
_输入计数器
num_get::do_get(iter_类型uu b,iter_类型u e,
ios_基地和iob,
ios_base::iostate&\uu err,
无效*&常数
{
//第一阶段
int _u基数=16;
//第二阶段
char_型__原子[26];
字符类型为0;
字符串分组;
使用面(uuuiob.getloc())。加宽(uunum\uget\ubase::uuusrc,
__num_get_base::__src+26,__原子);
字符串_; buf;
__buf.resize(uu buf.capacity());
char*uuu a=&uuuu buf[0];
字符*\uuuu a\u end=\uuuuu a;
未签名的uu g[u num_get_base::u num_get_buf_sz];
无符号*\uuu g\u end=\uuu g;
无符号uu dc=0;
对于(;uuu b!=uu e;++uu b)
{
如果(\uuuu a\u end==\uuuuu a+\uuu buf.size())
{
size_t_utmp=u buf.size();
__调整大小(2*uu buf.size());
__buf.resize(uu buf.capacity());
__a=&uuubuf[0];
__a_端=u a+u tmp;
}
如果(此->阶段2\u int\u循环(*uuuuuu b、\uuuu基、\uuuu a、\uuuu a\u端、\uuuu dc、,
__千年9月,分组,
__g、 _uug_end,uu原子)
打破
}
//第三阶段
__a[sizeof(_a)-1]=0;
#ifdef\u LIBCPP\u语言环境\u语言扩展
如果(sscanf_l(__a,_LIBCPP_GET_C_LOCALE,“%p,&_v)!=1)
#否则
如果(uuu sscanf_ul(uuu a,uuu cloc(),%p,&uuu v)!=1)
#恩迪夫
__err=ios_base::failbit;
//已检查EOF
如果(uuu b==uuu e)
__err |=ios_base::eofbit;
返回b;
}
对
// libstdc++
template<typename _CharT, typename _InIter>
_InIter
num_get<_CharT, _InIter>::
do_get(iter_type __beg, iter_type __end, ios_base& __io,
ios_base::iostate& __err, void*& __v) const
{
// Prepare for hex formatted input.
typedef ios_base::fmtflags fmtflags;
const fmtflags __fmt = __io.flags();
__io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
typedef __gnu_cxx::__conditional_type<(sizeof(void*)
<= sizeof(unsigned long)),
unsigned long, unsigned long long>::__type _UIntPtrType;
_UIntPtrType __ul;
__beg = _M_extract_int(__beg, __end, __io, __err, __ul);
// Reset from hex formatted input.
__io.flags(__fmt);
__v = reinterpret_cast<void*>(__ul);
return __beg;
}
//libstdc++
模板
_始作俑者
num_get::
得到(iter类型beg、iter类型end、ios基础和io、,
ios_base::iostate&__err,void*&_v)const
{
//准备十六进制格式的输入。
typedef ios_base::fmtflags fmtflags;
常量fmtflags uu fmt=uuu io.flags();
__io.flags((uu fmt&~ios_base::basefield)| ios_base::hex);
typedef\uu gnu\u cxx::\uu conditional\u type自209305版起,此问题已在libc++中解决。自209305版起,此问题已在libc++中解决。[locale.num.get]似乎是规定这一点的标准部分。它似乎说ss>>iptr
的行为应该像sscanf
和%p
一样,但AFAICS实际上没有规定这一点。“第3阶段”的描述似乎涵盖了整数和浮点类型,但忽略了void*
。它是sscanf(ss.str().c(str),%p“,&iptr);
work?@MattMcNabb:我刚刚用clang-503.0.38(基于LLVM 3.4svn)和sscanf(ss.str().c_str(),%p“,&iptr)进行了测试;
确实适合我。[locale.num.get]似乎是规定这一点的标准部分。它似乎说ss>>iptr
的行为应该像sscanf
和%p
一样,但AFAICS实际上没有规定这一点。“第3阶段”的描述似乎涵盖了整数和浮点类型,但它忽略了void*
。是sscanf(ss.str().c(str),”%p“,&iptr);
work?@mattmcnab:我刚刚用clang-503.0.38(基于LLVM 3.4svn)进行了测试,sscanf(ss.str().c_str(),%p“,&iptr);
确实对我有效。它们都一样