我无法使用sources/cxx-stl/llvm-libc++/src/string.cpp,它给出了有关未定义异常的一些错误。 所以我回到了to_string(int)。
但是在NDK源中,在sources/cxx-stl/llvm-libc++/src/string.cpp中,我找到了to_string(int)的实现,并尝试将其复制到我的代码中。 经过一些更正后,它起作用了。
所以我的最后一段代码是:
#include
#include
using namespace std;
template
inline
S
as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
{
typedef typename S::size_type size_type;
size_type available = s.size();
while (true)
{
int status = sprintf_like(&s[0], available + 1, fmt, a);
if ( status >= 0 )
{
size_type used = static_cast(status);
if ( used <= available )
{
s.resize( used );
break;
}
available = used; // Assume this is advice of how much space we need.
}
else
available = available * 2 + 1;
s.resize(available);
}
return s;
}
template ::value>
struct initial_string;
template
struct initial_string
{
string
operator()() const
{
string s;
s.resize(s.capacity());
return s;
}
};
template
struct initial_string
{
wstring
operator()() const
{
const size_t n = (numeric_limits::digits / 3)
+ ((numeric_limits::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
return s;
}
};
template
struct initial_string
{
wstring
operator()() const
{
wstring s(20, wchar_t());
s.resize(s.capacity());
return s;
}
};
string to_string(int val)
{
return as_string(snprintf, initial_string()(), "%d", val);
}