Neargye / magic_enum

Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
MIT License
4.88k stars 434 forks source link

enum literals with values > 127 #352

Closed smoehne closed 5 months ago

smoehne commented 5 months ago

The folowing code does not compile?

#include <magic_enum/magic_enum.hpp>
enum ErrorCodes
{
   WARN = -1,
   OK = 0,
   A = 127,
   B = 128,
   C = 0x4444
};

int main()
{
    constexpr std::size_t count = magic_enum::enum_count<ErrorCodes>();
    static_assert(count == 5);
}

image

Looks like literals with values > 127 will be ignored. is this intended?

Example in [GodBolt ](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:12,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:16,endLineNumber:13,positionColumn:1,positionLineNumber:13,selectionStartColumn:16,selectionStartLineNumber:13,startColumn:1,startLineNumber:13),source:'%23include+%22https://raw.githubusercontent.com/Neargye/magic_enum/master/include/magic_enum/magic_enum.hpp%22%0A%0Atemplate+%3Cauto+Start,+auto+End,+auto+Inc,+class+F%3E%0Aconstexpr+void+constexpr_for(F%26%26+f)%0A%7B%0A+++if+constexpr+(Start+%3C+End)%0A+++%7B%0A++++++f(std::integral_constant%3Cdecltype(Start),+Start%3E())%3B%0A++++++constexpr_for%3CStart+%2B+Inc,+End,+Inc%3E(f)%3B%0A+++%7D%0A%7D%3B%0A%0Aenum+ErrorCodes%0A%7B%0A+++WARN+%3D+-1,%0A+++OK+%3D+0,%0A+++A+%3D+0x00000001,%0A+++B+%3D+0x00000002,%0A+++C+%3D+0x80004002,%0A%7D%3B%0A%0Aint+main()%0A(%0A++++constexpr+std::size_t+count+%3D+magic_enum::enum_count%3CErrorCodes%3E()%3B%0A+++%0A+++constexpr+auto+names+%3D+magic_enum::enum_names%3CErrorCodes%3E()%3B%0A+++constexpr_for%3C0,+count,+1%3E(%5B%26%5D(auto+i)%0A+++%7B%0A++++++printf(%22%25s:%25d+%22,+names%5Bi%5D.data(),+magic_enum::enum_value%3CAPLHABITS,+i%3E())%3B%0A+++%7D)%3B%0A%0A)%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:63.15789473684211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:g132,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dc%2B%2B23+-Ofast+-Wall+-Wextra+-Weffc%2B%2B+-Werror+-pedantic+-pedantic-errors+-fno-exceptions',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+13.2+(Editor+%231)',t:'0'),(h:executor,i:(argsPanelShown:'1',compilationPanelShown:'0',compiler:g141,compilerName:'',compilerOutShown:'0',execArgs:'',execStdin:'',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dc%2B%2B17+',overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:'1',wrap:'1'),l:'5',n:'0',o:'Executor+x86-64+gcc+14.1+(C%2B%2B,+Editor+%231)',t:'0')),header:(),k:50.379073590561845,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+gcc+13.2',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gcc+13.2+(Compiler+%231)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:36.842105263157904,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Neargye commented 5 months ago

By default, only values within [-128, 127] are visible to magic_enum. More info here: https://github.com/Neargye/magic_enum/blob/master/doc/limitations.md#enum-range You can customize the allowed range globally (for all enums) or for each enum individually.