<div dir="auto"><div><br><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Sat, 11 Jan 2025, 13:55 tzsz via Std-Proposals, &lt;<a href="mailto:std-proposals@lists.isocpp.org">std-proposals@lists.isocpp.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello everyone,<br>
<br>
I&#39;d like to suggest an addition to functions /stoi/, /stol/ and /stoll/ <br>
defined in namespace /std/ in header /&lt;string&gt;/. As of right now these <br>
functions take either a /std::string const &amp; /or a /std::wstring const <br>
&amp;/ as their parameter. [1]<br>
<br>
Why:<br>
My current use case is that I need to parse data from a buffer. <br>
Currently my design for this is a component that manages the text buffer <br>
and just passed parts of it to smaller functions that do the actual <br>
processing. All those functions only take a /string_view/ as a parameter <br>
to keep it efficient. Some of these functions call others and only <br>
provide a small window into the original text. For this reason <br>
/std::sting const &amp;/ is not an option here. Because one function has to <br>
convert a part of the string into a number, I need to<br>
write code like this:<br>
<br>
int myint = std::stoi(std::string{myview});<br>
<br>
This works but is not very elegant because I involves a heap allocation.</blockquote></div></div><div dir="auto"><br></div><div dir="auto">The original reason for requiring a std::string is that stoi etc are defined in terms of strtoll which requires a null terminated string. A string_view isn&#39;t null terminated. </div><div dir="auto"><br></div><div dir="auto">The functions would need to be re-specified in terms of std::from_chars.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br>
I could also use the C versions that are used internally but then I&#39;d <br>
lose the nice C++ features of exceptions for conversion failures.<br>
<br>
To fix this I suggest the following addition to these functions:<br>
<br>
int stoi ( const std::string&amp; str, std::size_t* pos = nullptr, int base <br>
= 10 );     // current<br>
int stoi ( const std::wstring&amp; str, std::size_t* pos = nullptr, int base <br>
= 10 );    // current<br>
<br>
int stoi ( std::string_view&amp; str, std::size_t* pos = nullptr, int base = <br>
10 );      // new overload<br>
int stoi ( std::wstring_view&amp; str, std::size_t* pos = nullptr, int base <br>
= 10 );     // new overload<br>
<br>
The last two functions do not need an additional /const/ for the first <br>
parameter as /string_view/s are always read-only.<br>
<br>
There remains one issues here, the /stoi/ functions are defined in <br>
/&lt;string&gt;/ but it does not include /&lt;string_view&gt;/ and/&lt;string_view&gt;/ <br>
does not include /&lt;string&gt;/. I am not sure what the best way to settle <br>
this is. One way could be to split the overloads into two headers.<br>
<br>
I really hope this change could be included into the next standard.<br>
<br>
Thank you very much for reading<br>
<br>
<br>
[1] <a href="https://en.cppreference.com/w/cpp/string/basic_string/stol" rel="noreferrer noreferrer" target="_blank">https://en.cppreference.com/w/cpp/string/basic_string/stol</a><br>
<br>
-- <br>
Std-Proposals mailing list<br>
<a href="mailto:Std-Proposals@lists.isocpp.org" target="_blank" rel="noreferrer">Std-Proposals@lists.isocpp.org</a><br>
<a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" rel="noreferrer noreferrer" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a><br>
</blockquote></div></div></div>

