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

MAGIC_ENUM_SUPPORTED_ALIASES & magic_enum::enum_values<>() #68

Closed
Bktero opened this issue Dec 10, 2020 · 4 comments
Closed

MAGIC_ENUM_SUPPORTED_ALIASES & magic_enum::enum_values<>() #68

Bktero opened this issue Dec 10, 2020 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@Bktero
Copy link

Bktero commented Dec 10, 2020

Hello,

I wanted to write a piece of code that checks that all the values in an enum are unique.

Looking at the limitations of magic_enum, I read that "magic_enum won't work if a value is aliased". Well, that's what I want to do: detect if at least one value is aliased in my enum.

During my tests, I wrote the following code:

enum class Color {
	RED = 8, BLUE = 4, GREEN = 8
};

int main() {
#ifdef MAGIC_ENUM_SUPPORTED_ALIASES
	std::cout << "supported " << MAGIC_ENUM_SUPPORTED_ALIASES << '\n';
#else
	std::cout << "NOT supported" << '\n';
#endif

	auto colors = magic_enum::enum_values<Color>();

	for (auto& color:colors) {
		using namespace magic_enum::ostream_operators;
		std::cout << color << '\n';
	}

	return colors.size();
}

It prints:

supported 1
BLUE
RED

The problem is visible with Compiler Explorer using gcc 10.2: https://godbolt.org/z/4TTerr

The aliased value GREEN doesn't appears in the list returned by enum_values<Color>().

This seems to be a bug because MAGIC_ENUM_SUPPORTED_ALIASES is defined to be 1.

Am I missing something?

@Neargye
Copy link
Owner

Neargye commented Dec 10, 2020

MAGIC_ENUM_SUPPORTED_ALIASES macro says that aliases are generally supported.

if MAGIC_ENUM_SUPPORTED_ALIASES is set to 0, this behavior will occur

supported 0
BLUE

@Neargye
Copy link
Owner

Neargye commented Dec 10, 2020

In https://godbolt.org/z/4TTerr no bugs, bug is that the name of the macro is a little wrong.

@Neargye Neargye self-assigned this Dec 10, 2020
@Neargye Neargye added the question Further information is requested label Dec 10, 2020
@Bktero
Copy link
Author

Bktero commented Dec 11, 2020

Oh ok!

I was expecting:

supported 1
GREEN
BLUE
RED

I don't think the name of the macro is wrong, I think that the documentation is just incomplete. Maybe adding a more complete example will help? Maybe, the one we have in this issue?

@Neargye
Copy link
Owner

Neargye commented Dec 11, 2020

Yes, I will improve the documentation and then close these issues.

jamek pushed a commit to jamek/magic_enum that referenced this issue Feb 25, 2021
jamek added a commit to jamek/magic_enum that referenced this issue Feb 11, 2022
jamek added a commit to jamek/magic_enum that referenced this issue Feb 11, 2022
jamek pushed a commit to jamek/magic_enum that referenced this issue Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants