The Battle for Wesnoth  1.19.5+dev
from_chars.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include <charconv>
16 #include "utils/optional_fwd.hpp"
17 #include <string_view>
18 
19 namespace utils
20 {
21 template<typename T>
22 utils::optional<T> from_chars(std::string_view str, int base = 10)
23 {
24  static_assert(std::is_integral_v<T>, "Float support for charconv incomplete on current build requirements");
25  T result {};
26  const auto [_, ec] = std::from_chars(str.data(), str.data() + str.size(), result, base);
27  return ec == std::errc{} ? utils::make_optional(result) : utils::nullopt;
28 }
29 
30 } // namespace utils
static std::string _(const char *str)
Definition: gettext.hpp:93
utils::optional< T > from_chars(std::string_view str, int base=10)
Definition: from_chars.hpp:22