fixed_string
fixed_string copied to clipboard
Add numeric conversions
Suggested API:
// numeric conversions
template <size_t N>
constexpr int stoi(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr unsigned stou(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr long stol(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr unsigned long stoul(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr long long stoll(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr unsigned long long stoull(const fixed_string<N>& str, int base = 10);
template <size_t N>
constexpr float stof(const fixed_string<N>& str);
template <size_t N>
constexpr double stod(const fixed_string<N>& str);
template <size_t N>
constexpr long double stold(const fixed_string<N>& str);
template <auto val> // where decltype(val) is an integral type but not any of char types
constexpr fixed_string</*...*/> to_fixed_string() noexcept;
Proposed implementation: using to_chars
and from_chars
from <charconv>
Known issues: cannot be really constexpr
- [ ] Create implementation
- [ ] Create tests
- [ ] Add docs
Hi, i implemented stoi can i open pr for it?
@abel-mak sure!
@unterumarmung Hey, i didn't use from_chars in the implementation, must i use it?
@abel-mak
from_chars
is not constexpr
So:
- For C++17 we can use
from_chars
and doesn't allow to call our function in the constant context - For C++20 with
if (std::is_constant_evaluated())
we can use our own implementation that isconstexpr
and usefrom_chars
at the run-time
You can open the PR and I will take a look what have so far
@unterumarmung this what i've done so far https://github.com/abel-mak/fixed_string/commit/1f9725e5fd82ce697b4c607e0fd32ddf70c320d2
@abel-mak I've looked on what you already written Below you can find rules from C++ Core Guidelines that will make your code better:
- Do not declare before actual use (for example
sign
variable): https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-init - After applying Rule 1, it will be easier to apply this one: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rconst-immutable
- Also, please use
auto
for long type names
You can open the PR: it's easier to review code with opened pull request
Also, there's a tradeoff to be made with C++17, we either should:
- Provide not as effective
stoi
implementation for run-time but keep the ability to call the function at compile-time - Or remove
constexpr
in C++17 and provide effective run-time implementation withfrom_chars
There is no tradeoff in C++20 since we can distinguish run-time and compile-time with if (std::is_constant_evaluated())
@GeorgyFirsov @DymOK93 What do you think about all that (the code and the tradeoff)?
- It is unsafe to use
std::is*
functions without conversion to unsigned char: for example, see description of thestd::isspace
- I tried to use the
__is_constant_evaluated
compiler intrinsics, but they seem to have appeared much later than C++17. It might be wise to try to apply them, but in general, you will have to leave the C++17 version withoutconstexpr
. I'll think about this question... The answer depends on the most promising use of fixed_string
@unterumarmung hey, i opened PR and tried to add some changes from what you said about rules.