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

Categories

HOME
node.js
lucene
selenium-webdriver
firmware
hardware
netlogo
https
wildfly
fiware
scenekit
g2o
dropzone.js
instagram-api
geoserver
strongswan
crystal-reports-xi
apostrophe-cms
astyle
shogun
tablesorter
emu8086
ietf-netmod-yang
jquery-gmap3
pentaho-spoon
netbeans-7.3
pyvmomi
flip-flop
media
apprtcdemo
deprecation-warning
django-channels
omnipay
package-name
direct3d9
google-optimize
kernel-module
powerdesigner
oracle-data-modeler
meta-tags
scalacheck
google-console-developer
jquery-ui-widget-factory
behavior
proof-of-correctness
oltp
c++1z
lab-color-space
same-origin-policy
cscope
munin
google-chrome-arc
hashset
news-feed
pubsubhubbub
strstream
apple-watch-complication
agile-project-management
client-side
java-threads
reducers
mediafire
execution-time
spatialite
unexpectendoffile
smslib
setlocale
event-loop
magnetometer
centura
wpftoolkit
build-system
httplib
tracker
restkit-0.20
ntl
controltemplate
hierarchyid
eclipse-wtp
jnetpcap
batoo
trusted-timestamp
layar
raw-ethernet
iphone-sdk-4.1
set-include-path
mahjong
rebuild
zero-pad
remote-administration

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile