Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALLOW_FLAGS_FOR_ENUM in enum scope, allow inside classes #31

Open
martinus opened this issue Apr 12, 2020 · 2 comments
Open

ALLOW_FLAGS_FOR_ENUM in enum scope, allow inside classes #31

martinus opened this issue Apr 12, 2020 · 2 comments

Comments

@martinus
Copy link

martinus commented Apr 12, 2020

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

@NikolausDemmel
Copy link
Contributor

NikolausDemmel commented Apr 12, 2020

That would be nice.

Just curious, could you explain why the friend is needed?

Oh I get it now... It defines a friended non-member function.

@martinus
Copy link
Author

exactly. Also, if you just use static inline void enableEnumFlags(Name) {} instead of friend, then it's not possible to make the enum type private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants