fixed_string icon indicating copy to clipboard operation
fixed_string copied to clipboard

Add numeric conversions

Open unterumarmung opened this issue 3 years ago • 12 comments

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

unterumarmung avatar Aug 16 '21 17:08 unterumarmung

Hi, i implemented stoi can i open pr for it?

abel-mak avatar Aug 22 '21 17:08 abel-mak

@abel-mak sure!

unterumarmung avatar Dec 04 '21 15:12 unterumarmung

@unterumarmung Hey, i didn't use from_chars in the implementation, must i use it?

abel-mak avatar Jan 21 '22 17:01 abel-mak

@abel-mak from_chars is not constexpr So:

  1. For C++17 we can use from_chars and doesn't allow to call our function in the constant context
  2. For C++20 with if (std::is_constant_evaluated()) we can use our own implementation that is constexpr and use from_chars at the run-time

unterumarmung avatar Jan 21 '22 21:01 unterumarmung

You can open the PR and I will take a look what have so far

unterumarmung avatar Jan 21 '22 21:01 unterumarmung

@unterumarmung this what i've done so far https://github.com/abel-mak/fixed_string/commit/1f9725e5fd82ce697b4c607e0fd32ddf70c320d2

abel-mak avatar Jan 22 '22 11:01 abel-mak

@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:

  1. Do not declare before actual use (for example sign variable): https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-init
  2. After applying Rule 1, it will be easier to apply this one: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rconst-immutable
  3. Also, please use auto for long type names

unterumarmung avatar Jan 22 '22 15:01 unterumarmung

You can open the PR: it's easier to review code with opened pull request

unterumarmung avatar Jan 22 '22 15:01 unterumarmung

Also, there's a tradeoff to be made with C++17, we either should:

  1. Provide not as effective stoi implementation for run-time but keep the ability to call the function at compile-time
  2. Or remove constexpr in C++17 and provide effective run-time implementation with from_chars

There is no tradeoff in C++20 since we can distinguish run-time and compile-time with if (std::is_constant_evaluated())

unterumarmung avatar Jan 22 '22 15:01 unterumarmung

@GeorgyFirsov @DymOK93 What do you think about all that (the code and the tradeoff)?

unterumarmung avatar Jan 22 '22 15:01 unterumarmung

  1. It is unsafe to use std::is* functions without conversion to unsigned char: for example, see description of the std::isspace
  2. 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 without constexpr. I'll think about this question... The answer depends on the most promising use of fixed_string

DymOK93 avatar Jan 23 '22 17:01 DymOK93

@unterumarmung hey, i opened PR and tried to add some changes from what you said about rules.

abel-mak avatar Feb 05 '22 09:02 abel-mak