Skip to content

Commit

Permalink
fixes 'std::ptrdiff_t' to 'std::size_t' casting error and suppresses …
Browse files Browse the repository at this point in the history
…`-Wuseless-cast` warning for gcc and clang (Neargye#306)

* fixes 'std::ptrdiff_t' to 'std::size_t' casting error

addresses this compiler warning:

```text
[..]/magic_enum_utility.hpp:101:31: warning: conversion to ‘std::size_t’ {aka ‘long unsigned int’} from ‘std::ptrdiff_t’ {aka ‘long int’} may change the sign of the result [-Wsign-conversion]
  101 |       return enum_value<D, S>(index);
      |                               ^~~~~
```

* suppresses `-Wuseless-cast` for static_cast<char_type>('\0')

needed in case 'char_type' is 'char' (common on Linux but rare on Windows?)

```text
[..]/magic_enum.hpp:275:114: warning: useless cast to type ‘using magic_enum::char_type = using std::basic_string_view<char>::value_type = char’ {aka ‘char’} [-Wuseless-cast]
  275 |   constexpr static_str(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., static_cast<char_type>('\0')} {}
      |                                                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
  • Loading branch information
RalphSteinhagen authored Nov 9, 2023
1 parent 49bb72d commit 5cf4eb3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/magic_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-warning-option"
# pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
# pragma clang diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast<char_type>('\0')' for char_type = char (common on Linux).
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'.
# pragma GCC diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast<char_type>('\0')' for char_type = char (common on Linux).
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 26495) // Variable 'static_str<N>::chars_' is uninitialized.
Expand Down
8 changes: 4 additions & 4 deletions include/magic_enum_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
if (const auto i = enum_index<D, S>(value)) {
const std::ptrdiff_t index = (static_cast<std::ptrdiff_t>(*i) + n);
if (index >= 0 && index < count) {
return enum_value<D, S>(index);
return enum_value<D, S>(static_cast<std::size_t>(index));
}
}
return {};
Expand All @@ -98,7 +98,7 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
if (const auto i = enum_index<D, S>(value)) {
const std::ptrdiff_t index = ((((static_cast<std::ptrdiff_t>(*i) + n) % count) + count) % count);
if (index >= 0 && index < count) {
return enum_value<D, S>(index);
return enum_value<D, S>(static_cast<std::size_t>(index));
}
}
return MAGIC_ENUM_ASSERT(false), value;
Expand All @@ -112,7 +112,7 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
if (const auto i = enum_index<D, S>(value)) {
const std::ptrdiff_t index = (static_cast<std::ptrdiff_t>(*i) - n);
if (index >= 0 && index < count) {
return enum_value<D, S>(index);
return enum_value<D, S>(static_cast<std::size_t>(index));
}
}
return {};
Expand All @@ -126,7 +126,7 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
if (const auto i = enum_index<D, S>(value)) {
const std::ptrdiff_t index = ((((static_cast<std::ptrdiff_t>(*i) - n) % count) + count) % count);
if (index >= 0 && index < count) {
return enum_value<D, S>(index);
return enum_value<D, S>(static_cast<std::size_t>(index));
}
}
return MAGIC_ENUM_ASSERT(false), value;
Expand Down

0 comments on commit 5cf4eb3

Please sign in to comment.