Skip to content

ALLOW_FLAGS_FOR_ENUM in enum scope, allow inside classes #31

@martinus

Description

@martinus

Currently the ALLOW_FLAGS_FOR_ENUM has to be in global scope to work, which can be a bit cumbersome (see #26). Instead, I propose two different macros:

#define ALLOW_FLAGS_FOR_ENUM(Name) inline void enableEnumFlags(Name) {}
#define ALLOW_FLAGS_FOR_ENUM_IN_CLASS(Name) friend inline void enableEnumFlags(Name) {}

Which works nicely thanks to ADL with this is_flags implementation:

template <typename T, typename = void>
struct is_flags : std::false_type {};
template <typename T>
struct is_flags<T, decltype(enableEnumFlags(T{}))> : std::true_type {};

It is then possible (mandatory) to use ALLOW_FLAGS_FOR_ENUM(Whatever) directly after an enum is defined, whatever scope it may be, and ALLOW_FLAGS_FOR_ENUM_IN_CLASS(Whatever) if the enum is inside a class or struct.

Note that the enableEnumFlags name should be rather unique so it is not used by anyone else, so maybe it is a good idea to obfuscate it a bit more with e.g. a random number, e.g. enableFlagsfbc4db870f32e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions