Date: Sat, 11 Jan 2025 14:55:30 +0100
Hello everyone,
I'd like to suggest an addition to functions /stoi/, /stol/ and /stoll/
defined in namespace /std/ in header /<string>/. As of right now these
functions take either a /std::string const & /or a /std::wstring const
&/ as their parameter. [1]
Why:
My current use case is that I need to parse data from a buffer.
Currently my design for this is a component that manages the text buffer
and just passed parts of it to smaller functions that do the actual
processing. All those functions only take a /string_view/ as a parameter
to keep it efficient. Some of these functions call others and only
provide a small window into the original text. For this reason
/std::sting const &/ is not an option here. Because one function has to
convert a part of the string into a number, I need to
write code like this:
int myint = std::stoi(std::string{myview});
This works but is not very elegant because I involves a heap allocation.
I could also use the C versions that are used internally but then I'd
lose the nice C++ features of exceptions for conversion failures.
To fix this I suggest the following addition to these functions:
int stoi ( const std::string& str, std::size_t* pos = nullptr, int base
= 10 ); // current
int stoi ( const std::wstring& str, std::size_t* pos = nullptr, int base
= 10 ); // current
int stoi ( std::string_view& str, std::size_t* pos = nullptr, int base =
10 ); // new overload
int stoi ( std::wstring_view& str, std::size_t* pos = nullptr, int base
= 10 ); // new overload
The last two functions do not need an additional /const/ for the first
parameter as /string_view/s are always read-only.
There remains one issues here, the /stoi/ functions are defined in
/<string>/ but it does not include /<string_view>/ and/<string_view>/
does not include /<string>/. I am not sure what the best way to settle
this is. One way could be to split the overloads into two headers.
I really hope this change could be included into the next standard.
Thank you very much for reading
[1] https://en.cppreference.com/w/cpp/string/basic_string/stol
I'd like to suggest an addition to functions /stoi/, /stol/ and /stoll/
defined in namespace /std/ in header /<string>/. As of right now these
functions take either a /std::string const & /or a /std::wstring const
&/ as their parameter. [1]
Why:
My current use case is that I need to parse data from a buffer.
Currently my design for this is a component that manages the text buffer
and just passed parts of it to smaller functions that do the actual
processing. All those functions only take a /string_view/ as a parameter
to keep it efficient. Some of these functions call others and only
provide a small window into the original text. For this reason
/std::sting const &/ is not an option here. Because one function has to
convert a part of the string into a number, I need to
write code like this:
int myint = std::stoi(std::string{myview});
This works but is not very elegant because I involves a heap allocation.
I could also use the C versions that are used internally but then I'd
lose the nice C++ features of exceptions for conversion failures.
To fix this I suggest the following addition to these functions:
int stoi ( const std::string& str, std::size_t* pos = nullptr, int base
= 10 ); // current
int stoi ( const std::wstring& str, std::size_t* pos = nullptr, int base
= 10 ); // current
int stoi ( std::string_view& str, std::size_t* pos = nullptr, int base =
10 ); // new overload
int stoi ( std::wstring_view& str, std::size_t* pos = nullptr, int base
= 10 ); // new overload
The last two functions do not need an additional /const/ for the first
parameter as /string_view/s are always read-only.
There remains one issues here, the /stoi/ functions are defined in
/<string>/ but it does not include /<string_view>/ and/<string_view>/
does not include /<string>/. I am not sure what the best way to settle
this is. One way could be to split the overloads into two headers.
I really hope this change could be included into the next standard.
Thank you very much for reading
[1] https://en.cppreference.com/w/cpp/string/basic_string/stol
Received on 2025-01-11 13:55:35