/
Ikocs
/
cpp-tasks
Обзор
Документация
Войти
/
Ikocs
/
cpp-tasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
53_std_from_chars/main.cpp
34 строки
774 B
ikocs
Add 53
15 окт 2024, 10:21
15 окт 2024, 10:21
4f17c93
Код
Авторство
О чём код?
/// URL ref: https://www.cppstories.com/2018/12/fromchars/ #include <charconv> // from_char, to_char #include <string> #include <iostream> #include <optional> #include <string_view> constexpr std::optional<int> to_int(std::string_view sv) { int value {}; const auto ret = std::from_chars(sv.begin(), sv.end(), value); if (ret.ec == std::errc{}) return value; return {}; } std::optional<double> to_double(std::string_view sv) { double value {}; const auto ret = std::from_chars(sv.begin(), sv.end(), value); if (ret.ec == std::errc{}) return value; return {}; } int main() { static_assert(to_int("hello") == std::nullopt); static_assert(to_int("10") == 10); std::cout << to_double("1.123").value() << "\n"; }