Common/StringUtil: Support compiling on gcc7

This commit is contained in:
Connor McLaughlin 2020-02-28 17:00:16 +10:00
parent bbdee22ea8
commit 122726fe65
1 changed files with 17 additions and 2 deletions

View File

@ -1,11 +1,16 @@
#pragma once
#include <charconv>
#include <cstdarg>
#include <cstddef>
#include <cstring>
#include <optional>
#include <string>
#if __cplusplus >= 201703L
#include <charconv>
#else
#include <sstream>
#endif
namespace StringUtil {
/// Constructs a std::string from a format string.
@ -33,9 +38,19 @@ template<typename T>
std::optional<T> FromChars(const std::string_view str)
{
T value;
#if __cplusplus >= 201703L
T value;
const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value);
if (result.ec != std::errc())
return std::nullopt;
#else
std::string temp(str);
std::istringstream ss(temp);
ss >> value;
if (ss.fail())
return std::nullopt;
#endif
return value;
}