Request for comments: to_static_string
Hi all, Since we will be reviewing Boost.CharConv shortly, and my development efforts in Boost.MySQL are currently requiring converting numbers to strings, I've been playing with the utilities we currently have to do so. I've found cases where I just needed to convert something to string, then pass the result to another function taking a string_view as parameter. The most efficient and correct way to do so is using to_chars (stack based, non-throwing, C++23 constexpr and locale-independent). But I still like the simplicity of std::to_string. Since I had some free time this weekend, I took a step forward and implemented a (deadly simple) wrapper around to_chars that looks like std::to_string, but has all the benefits of to_chars. I called this to_static_string. Repo is here: https://github.com/anarthal/to-static-string This is currently a prototype. I want to hear everyone's opinion on this - do you think this may be useful to any of you? I don't think it's big enough to even consider putting it into Boost (my idea is having it as a standalone lib). Any comments are welcome. Thanks, Ruben.
Ruben Perez wrote:
Hi all,
Since we will be reviewing Boost.CharConv shortly, and my development efforts in Boost.MySQL are currently requiring converting numbers to strings, I've been playing with the utilities we currently have to do so.
I've found cases where I just needed to convert something to string, then pass the result to another function taking a string_view as parameter. The most efficient and correct way to do so is using to_chars (stack based, non-throwing, C++23 constexpr and locale-independent). But I still like the simplicity of std::to_string.
Since I had some free time this weekend, I took a step forward and implemented a (deadly simple) wrapper around to_chars that looks like std::to_string, but has all the benefits of to_chars. I called this to_static_string. Repo is here:
https://github.com/anarthal/to-static-string
This is currently a prototype. I want to hear everyone's opinion on this - do you think this may be useful to any of you? I don't think it's big enough to even consider putting it into Boost (my idea is having it as a standalone lib). Any comments are welcome.
https://www.boost.org/doc/libs/1_84_0/libs/static_string/doc/html/static_str...
https://www.boost.org/doc/libs/1_84_0/libs/static_string/doc/html/static_str...
I didn't know this was a thing. It looks like it internally uses snprintf though. Are there plans to update this to use Boost.Charconv if it gets accepted? (Although this technically means changing its behavior).
Ruben Perez wrote:
https://www.boost.org/doc/libs/1_84_0/libs/static_string/doc/html/stat ic_string/ref/boost__static_strings__to_static_string.html
I didn't know this was a thing. It looks like it internally uses snprintf though.
That's an interesting point. It means that it's locale-dependent, and probably shouldn't be. I don't think any of the users of it would expect that.
participants (2)
-
Peter Dimov
-
Ruben Perez