c++
Access enum entries from anonymous struct
I've a code like this: struct { enum { entry, } en; } data; void foo() { switch(data.en) { } } that gives me a warning: main.cpp:13:11: warning: enumeration value 'entry' not handled in switch [-Wswitch] switch(data.en) which is expected. I'm curious if I can add case entry: without making my struct named one (which obviously works). This: struct { enum { entry, } en; } data; void foo() { switch(data.en) { case entry: break; } } gives an error + warning: main.cpp: In function 'void foo()': main.cpp:15:14: error: 'entry' was not declared in this scope case entry: ^~~~~ main.cpp:13:11: warning: enumeration value 'entry' not handled in switch [-Wswitch] switch(data.en) ^
You can write: case decltype(data.en)::entry: however I think it would not be considered good code.
In C you can do it the following way #include <stdio.h> struct { enum { entry, } en; } data = { entry }; void foo() { switch ( data.en ) { case entry: puts( "Hello, World!" ); break; } } int main( void ) { foo(); } In C++ you do it the following way #include <iostream> struct { enum { entry, } en; } data = { decltype( data.en )::entry }; void foo() { switch ( data.en ) { case data.entry: std::cout << "Hello, World!" << std::endl; break; } } int main() { foo(); }
Related Links
How to debug the string handling in my C++ program? [duplicate]
How to properly convert a vector<int> to a void* and back to a vector<int>?
Pointer array always generate one even out of index C++
Missing notification of condition variable
Qt weird priority for mouse control
Different key type in a map
Take input in arrays of pointers
CopyFile API function not working after using GetOpenFileName to get the file path
2 classes in 2 header files
Custom type Matrix*=int in Eigen
Visual Studio - Find and Replace but not an exact match?
Opencv source compile with cuda generates multiple definition link error
printf the value of %n in the same call - senseless?
What's the difference between atomic.store and atomic_thread_fence?
C++ Iterator List
std type_traits conflict with Qt type_traits