diff --git a/include/toolkit.h b/include/toolkit.h index 9f34ae6..7c9a9e6 100644 --- a/include/toolkit.h +++ b/include/toolkit.h @@ -3,6 +3,8 @@ #include #include +#include +#include //use for to_chars constexpr size_t buffer_size = 32; @@ -18,6 +20,18 @@ namespace toolkit{ } return std::string(buffer, res.ptr - buffer); } + + template// requires std::is_same_v + std::expected stoi(const std::string& str){ + T value; + + auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value); + if(res.ec != std::errc()){ + return std::unexpected(std::make_error_code(res.ec).message()); + } + return value; + + } } #endif diff --git a/src/main.cpp b/src/main.cpp index 05f7712..4ad276d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,8 @@ using namespace std; int main(int argc, char** argv) { - double num = .2; - auto str = toolkit::itos(num); - cout<