std::to_string, std::sto*, and custom allocators

250 views
Skip to first unread message

sean.mid...@gmail.com

unread,
May 14, 2015, 9:15:06 PM5/14/15
to std-dis...@isocpp.org
Is there a particularly strong reason that std::to_string and std::sto* functions only work explicitly with std::string?

Our game engine has a type alias of std::string to one using a custom allocator for accounting reasons. I can understand to an extent why this doesn't work with to_string (it'd have to be a template and take an allocator as a template parameter) but less so for the std::sto* versions (which basically just forward to the C versions anyway).

Neither are hard to rewrite since they're mostly just wrappers anyway, but it's a bit annoying that I have to do so.

If there's not a good reason, I'll toss this on my stack of proposals to write, I guess. :/

Thiago Macieira

unread,
May 15, 2015, 1:28:52 PM5/15/15
to std-dis...@isocpp.org
You can change the allocator, but not the character type.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358

Nicol Bolas

unread,
May 15, 2015, 1:50:08 PM5/15/15
to std-dis...@isocpp.org


On Friday, May 15, 2015 at 1:28:52 PM UTC-4, Thiago Macieira wrote:
On Thursday 14 May 2015 18:15:06 sean.mid...@gmail.com wrote:
> Is there a particularly strong reason that std::to_string and std::sto*
> functions only work explicitly with std::string?
>
> Our game engine has a type alias of std::string to one using a custom
> allocator for accounting reasons. I can understand to an extent why this
> doesn't work with to_string (it'd have to be a template and take an
> allocator as a template parameter) but less so for the std::sto* versions
> (which basically just forward to the C versions anyway).
>
> Neither are hard to rewrite since they're mostly just wrappers anyway, but
> it's a bit annoying that I have to do so.
>
> If there's not a good reason, I'll toss this on my stack of proposals to
> write, I guess. :/

You can change the allocator, but not the character type.

No, you cannot. std::stod and its variations are specific to std::string and std::wstring. They are not templated on the allocator types, so you can't pass a std::basic_string that uses a different allocator, even if it uses the same character type. This is true of C++11 and C++14.

This would be a good time for someone to come up with a proposal to fix these things, as well as add variations that take string_view and whatever.

Thiago Macieira

unread,
May 15, 2015, 4:34:20 PM5/15/15
to std-dis...@isocpp.org
On Friday 15 May 2015 10:50:08 Nicol Bolas wrote:
> > You can change the allocator, but not the character type.
>
> No, you cannot.

I'm not sure you're agreeing with me (can't change the character type) or
disagreeing (can't change the allocator).

I think I didn't express myself well.

Right now, you can't change either without writing wrappers or your own
conversion code. You should be able to change the allocator because, after
all, there's no allocation done.

> std::stod and its variations are specific to std::string
> and std::wstring. They are not templated on the allocator types, so you
> can't pass a std::basic_string that uses a different allocator, even if it
> uses the same character type. This is true of C++11 and C++14.
>
> This would be a good time for someone to come up with a proposal to fix
> these things, as well as add variations that take string_view and whatever.

Agreed.
Reply all
Reply to author
Forward
0 new messages