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

nested magic_enum::enum_switch -- failed on msvc? #200

Open
Korsar13 opened this issue Jul 20, 2022 · 4 comments
Open

nested magic_enum::enum_switch -- failed on msvc? #200

Korsar13 opened this issue Jul 20, 2022 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@Korsar13
Copy link

https://gist.github.com/Korsar13/5c171d0cd0b47d62276cac07b6b6a184

compiled by clang, prints

convert from char to char
convert from char to int
convert from char to float
convert from int to char
convert from int to int
convert from int to float
convert from float to char
convert from float to int
convert from float to float

msvc compilation fails,
mm.cpp(39): error C2893: Failed to specialize function template 'void convert_tmpl(const TSrc *,TDst *,size_t)'

@Korsar13
Copy link
Author

initial code (compiled by msvc without errors, but wrong result) -- https://gist.github.com/Korsar13/f993beaa52947a0ce42a632602e2aecf

@schaumb
Copy link
Contributor

schaumb commented Jul 21, 2022

Hi!

The problem here is not the magic_enum library, but that MSVC handles the nested lambdas.
As you can see, if the nested enum_switch not use the outside type, the code compiles.

Probably the compiler wants to simplify lambdas instantiation, that's why It don't want to create new lambda types for all enum / different usings.

This problem can be eliminated if you use not nested lambda, but some struct.
https://godbolt.org/z/4aYezTzvf

@schaumb
Copy link
Contributor

schaumb commented Jul 21, 2022

From the library side:

This issue can be eliminated with the following techniques:

  • make magic_enum::enum_for_each and magic_enum::enum_switch to handle variadic arguments, and generate all enum combination calls, or the multiple switch call. The enum_switch implementation will be tricky.
    These calls can be slow at compile time.
  • create magic_enum::bind_front constexpr function for backward compatible way to c++17

@Neargye Neargye added help wanted Extra attention is needed invalid This doesn't seem right labels Sep 2, 2022
@Neargye Neargye removed the invalid This doesn't seem right label May 21, 2023
@Neargye
Copy link
Owner

Neargye commented Jun 12, 2023

@schaumb
I added a new header magic_enum_utility.hpp for different secondary functions, maybe add magic_enum::bind_front there?
and add some example and note in doc

magic_enum::bind_front seems like a cheaper option to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants