Skip to content

Commit

Permalink
convert: don't use concepts for older compilers
Browse files Browse the repository at this point in the history
Concepts are supported on Clang >=10, GCC >= 10 and MSVC >= 19.23.

This will not be so robust, but should work for our needs.

Fixes: #199
  • Loading branch information
kasper93 committed Sep 15, 2023
1 parent 0b4735c commit dde4e7c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ static int ccStrPrintDouble( char *str, int bufsize, int decimals, double value
namespace {

template <typename T>
concept has_std_to_chars = requires(char *begin, char *end, T &n)
{
std::to_chars(begin, end, n);
struct has_std_to_chars_impl {
template <typename CT>
static auto _(CT s) -> decltype(std::to_chars(s, s, std::declval<T>()), std::true_type{});
static auto _(...) -> std::false_type;
static constexpr bool value = decltype(_((char *){}))::value;
};

template <typename T>
constexpr bool has_std_to_chars = has_std_to_chars_impl<T>::value;

template <typename T, typename... Args>
static inline int to_chars(char *buf, size_t len, T n, Args ...args)
{
Expand All @@ -53,11 +58,16 @@ static inline int to_chars(char *buf, size_t len, T n, Args ...args)
}

template <typename T>
concept has_std_from_chars = requires(const char *begin, const char *end, T &n)
{
std::from_chars(begin, end, n);
struct has_std_from_chars_impl {
template <typename CT>
static auto _(CT s) -> decltype(std::from_chars(s, s, std::declval<T&>()), std::true_type{});
static auto _(...) -> std::false_type;
static constexpr bool value = decltype(_((const char *){}))::value;
};

template <typename T>
constexpr bool has_std_from_chars = has_std_from_chars_impl<T>::value;

template <typename T, typename... Args>
static inline bool from_chars(pl_str str, T &n, Args ...args)
{
Expand Down

0 comments on commit dde4e7c

Please sign in to comment.