[Interest] How to determine at compile time that given enum type is Q_ENUMed?

Alexey Rochev equeim at gmail.com
Sat Aug 20 13:13:18 CEST 2022


Kinda figured out how to do this:

```
template<typename T, typename = void>
inline constexpr bool isQEnum = false;

template<typename T>
inline constexpr bool isQEnum<T,
std::void_t<decltype(qt_getEnumMetaObject(std::declval<T>()))>> =
true;
```
It's not the best approach since it uses undocumented Q_ENUM
internals, but it's probably stable (I'm not sure if
qt_getEnumMetaObject functions are part of ABI though).

ср, 17 авг. 2022 г. в 00:19, Alexey Rochev <equeim at gmail.com>:
>
> Hello
> I'm trying to specialize fmt::formatter for all Q_ENUM enums types,
> and for that I need to come up with constexpr bool function/variable
> template that somehow checks if Q_ENUM was declared for enum (to use
> with SFINAE).
> I know that QMetaEnum::fromType() fails with static_assert if Q_ENUM
> is not declared for type, which means that this information is
> available at compile time. But I'm not experienced enough with
> templates to derive boolean constant from this (without using private
> Qt internals). Is it possible to "convert" function template failing
> with static_assert to boolean compile time constant (i.e. extract
> condition from outside)?


More information about the Interest mailing list