Fix build on MinGW
For some reason MinGW doesn't like using template argument deduction on the array... Make it explicit when building there. Also moves the `sized_array` alias to `enum_base` since we don't need it defined in the definition class. Also makes the two versions of ENUM_AND_ARRAY cleaner.
This commit is contained in:
parent
31b508ccfc
commit
5b846fea3c
1 changed files with 18 additions and 11 deletions
|
@ -28,10 +28,10 @@ namespace string_enums
|
|||
* The number of enum values must match the number of elements in the @a values array.
|
||||
* The values the @a values array must be unique.
|
||||
*/
|
||||
template<typename T>
|
||||
struct enum_base : public T
|
||||
template<typename Definition>
|
||||
struct enum_base : public Definition
|
||||
{
|
||||
using enum_type = typename T::type;
|
||||
using enum_type = typename Definition::type;
|
||||
|
||||
// check that all implementations of this are scoped enums
|
||||
// TODO: C++23 std::is_scoped_enum
|
||||
|
@ -45,7 +45,7 @@ struct enum_base : public T
|
|||
*/
|
||||
static std::string get_string(enum_type key)
|
||||
{
|
||||
return std::string{T::values[static_cast<int>(key)]};
|
||||
return std::string{Definition::values[static_cast<int>(key)]};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,7 +57,7 @@ struct enum_base : public T
|
|||
static constexpr std::optional<enum_type> get_enum(const std::string_view value)
|
||||
{
|
||||
for(unsigned int i = 0; i < size(); i++) {
|
||||
if(value == T::values[i]) {
|
||||
if(value == Definition::values[i]) {
|
||||
return static_cast<enum_type>(i);
|
||||
}
|
||||
}
|
||||
|
@ -84,16 +84,23 @@ struct enum_base : public T
|
|||
*/
|
||||
static constexpr std::size_t size()
|
||||
{
|
||||
return T::values.size();
|
||||
return Definition::values.size();
|
||||
}
|
||||
|
||||
/** Provide a alias template for an array of matching size. */
|
||||
template<typename T>
|
||||
using sized_array = std::array<T, size()>;
|
||||
};
|
||||
|
||||
#ifndef __MINGW64__
|
||||
#define ENUM_AND_ARRAY(...) \
|
||||
enum class type { __VA_ARGS__ }; \
|
||||
static constexpr std::array values{__VA_ARGS__}; \
|
||||
\
|
||||
/** Provide a alias template for an array of matching size. */ \
|
||||
template<typename T> \
|
||||
using sized_array = std::array<T, values.size()>;
|
||||
static constexpr std::array values{__VA_ARGS__};
|
||||
#else
|
||||
#define ENUM_AND_ARRAY(...) \
|
||||
enum class type { __VA_ARGS__ }; \
|
||||
static constexpr std::array<std::string_view, std::tuple_size_v<decltype(std::make_tuple(__VA_ARGS__))>> \
|
||||
values{__VA_ARGS__};
|
||||
#endif
|
||||
|
||||
} // namespace string_enums
|
||||
|
|
Loading…
Add table
Reference in a new issue