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

Explicitly mark the result of detail::names as constexpr #305

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

vt4a2h
Copy link
Contributor

@vt4a2h vt4a2h commented Nov 6, 2023

It might make no sense, but on MSVC it can generate a compile-time error, especially if an enumerator's value is out of range. However, it compiles fine with clang on Mac, Linux, and Windows. Ditto for GCC on Linux. It seems that only MSVC on Windows causes the issue.
If there are other fixes for this issue I'd be happy to have them instead of this one :)

Example:

[39/44] Building CXX object test\CMakeFiles\test-cpp20.dir\test.cpp.obj
FAILED: test/CMakeFiles/test-cpp20.dir/test.cpp.obj 
C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1436~1.325\bin\Hostx64\x64\cl.exe  /nologo /TP  -IC:\Users\Vitaly\CLionProjects\magic_enum\test\3rdparty\Catch2\include -IC:\Users\Vitaly\CLionProjects\magic_enum\include /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /W4 /WX /permissive- /std:c++20 /showIncludes /Fotest\CMakeFiles\test-cpp20.dir\test.cpp.obj /Fdtest\CMakeFiles\test-cpp20.dir\ /FS -c C:\Users\Vitaly\CLionProjects\magic_enum\test\test.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): error C3615: constexpr function 'magic_enum::detail::names' cannot result in a constant expression
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: failure was caused by call of undefined function or one not declared 'constexpr'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: see usage of '__builtin_array_init_helper'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: the call stack of the evaluation (the oldest call first) is
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: while evaluating function 'std::array<std::string_view,0> magic_enum::detail::names<D,magic_enum::detail::enum_subtype::common,>(std::integer_sequence<size_t>) noexcept'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(1284): note: see reference to variable template 'const std::array<std::string_view,0> names_v<enum MyFoo,0>' being compiled
C:\Users\Vitaly\CLionProjects\magic_enum\test\test.cpp(520): note: see reference to function template instantiation 'std::basic_string_view<char,std::char_traits<char>> magic_enum::enum_name<MyFoo,magic_enum::detail::enum_subtype::common>(E) noexcept' being compiled
        with
        [
            E=MyFoo
        ]
[40/44] Building CXX object test\CMakeFiles\test-cpp17.dir\test.cpp.obj
cl : Command line warning D9025 : overriding '/W3' with '/W4'
[41/44] Building CXX object test\CMakeFiles\test-cpplatest.dir\test.cpp.obj
FAILED: test/CMakeFiles/test-cpplatest.dir/test.cpp.obj 
C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1436~1.325\bin\Hostx64\x64\cl.exe  /nologo /TP  -IC:\Users\Vitaly\CLionProjects\magic_enum\test\3rdparty\Catch2\include -IC:\Users\Vitaly\CLionProjects\magic_enum\include /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /W4 /WX /permissive- /std:c++latest /showIncludes /Fotest\CMakeFiles\test-cpplatest.dir\test.cpp.obj /Fdtest\CMakeFiles\test-cpplatest.dir\ /FS -c C:\Users\Vitaly\CLionProjects\magic_enum\test\test.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): error C3615: constexpr function 'magic_enum::detail::names' cannot result in a constant expression
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: failure was caused by call of undefined function or one not declared 'constexpr'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: see usage of '__builtin_array_init_helper'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: the call stack of the evaluation (the oldest call first) is
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(834): note: while evaluating function 'std::array<std::string_view,0> magic_enum::detail::names<D,magic_enum::detail::enum_subtype::common,>(std::integer_sequence<size_t>) noexcept'
C:\Users\Vitaly\CLionProjects\magic_enum\include\magic_enum.hpp(1284): note: see reference to variable template 'const std::array<std::string_view,0> names_v<enum MyFoo,0>' being compiled
C:\Users\Vitaly\CLionProjects\magic_enum\test\test.cpp(520): note: see reference to function template instantiation 'std::basic_string_view<char,std::char_traits<char>> magic_enum::enum_name<MyFoo,magic_enum::detail::enum_subtype::common>(E) noexcept' being compiled
        with

It might make no sense, but on MSVC it can generate a compile-time
error, especially if an enumerator's value is out of range.

Example:
error C3615: constexpr function 'magic_enum::detail::names' cannot
result in a constant expression
...
note: failure was caused by call of undefined function or one not
declared 'constexpr'
...
note: see usage of '__builtin_array_init_helper'
@Neargye
Copy link
Owner

Neargye commented Nov 9, 2023

Thanks!

@Neargye Neargye merged commit 5523803 into Neargye:master Nov 9, 2023
18 checks passed
@Neargye Neargye added this to the v0.9.4 milestone Nov 12, 2023
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

Successfully merging this pull request may close these issues.

2 participants