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 is calling a function on a subtype implemented?
Model fitting in ceres_solver
When does PC allocate static char* inside of if block [closed]
Read from a file search for a word and copy the entire line to another file [closed]
What is the difference between these different ways to invoke an operator on `this`?
Saving variables after a form is closed in WinForms with C++
How to call OnEraseBkgnd() to repaint the window in another function? (MFC)
How can I convert this C# foreach code to C++?
Dynamic int pointer address? [duplicate]
x86 MASM - passing and accessing a 2D array
Boost.spirit x3 compiler error when using semantic action in VS15
g++ undefined reference with a shared library [duplicate]
How can I pass an ifstream as argument to std::thread function?
File handling in c++ to read two integers
linux scheduling to strictly prefer higher priority thread than low priority thread
Returning reference using a ternery expression causes runtime error

Categories

HOME
excel
docker
java-ee
momentjs
.net-core
dropbox-api
datastax
properties
iframe
migration
https
modularity
text
basecamp
linux-device-driver
dropzone.js
speech-recognition
contiki
glusterfs
google-vr
uinavigationcontroller
css-modules
shoutem
benchmarking
libreoffice-base
bit-manipulation
systemml
web-component
pad
pyside
flip-flop
xfce
coap
bug-tracking
entity-relationship
sandbox
ebcdic
android-appbarlayout
bing-api
mahapps.metro
spring-4
ipc
drools-fusion
vim-syntax-highlighting
kinvey
handbrake
swiftmailer
each
data-storage
xfs
inline
parasoft
modelio
enterprise-library-6
o365rwsclient
silent-installer
coupling
heyzap
fuzzy
autocompleteextender
shodan
banking
self-referencing-table
z-order
decompression
spock-reports
reportviewer2008
webtorrent
apple-watch-complication
owner
nested-forms
numerical-analysis
nibble
groovy-eclipse
unreal-blueprint
ionic-view
mime-mail
sonarqube-5.4
liquibase-hibernate
mongoose-web-server
irs
mindbody
johnny-five
tess-two
msbuildcommunitytasks
angularjs-ng-options
window-position
xlet
materialized-views
order-of-operations
cordova-chrome-app
pixman
hashbytes
geographical-information
jquery-dropkick
centura
review
gliffy
mooc
ember.js-view
orca
angularjs-ng-show
jform-designer
mindmap
tiles2
fubumvc
gideros
iunknown
payflowlink
ipad-2
sequence-alignment
ejbql
system-design
twisted.internet
actinic
flotr
continuations
tao
context-sensitive-help
httpverbs
wss-3.0
loose-coupling
installshield-2008
pvcs
flashlite

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App