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

C++ range for loop different than regular for loop [closed]
C++ boost thread causes segmentation fault when instantiated twice
c++ - 3 bears in a vector of a struct, how can I change this code so the specific bear at the exit is removed?
Reading from excel/text file without skipping whitespaces unless made by a tab
How to initialize const size array in constructor without explicitly stating the size in class declaration?
Reading txt file into 2d array C++
How can I mark a function with an attribute conditionally on the value of a parameter?
Read a text file and store into two different arrays: a string array and float array
converting constructor: How do you explain a function given different arguments in c++
Background in Qt multiplies instead of fitting into window size
C++ pause this program using cin.get()
linking error in utility::conversions::to_string_t in qt creator
readLine() not being saved properly
Check socket with select() before using send()
Error: “Function” not declared in this scope
syntax for array of pointers in C++

Categories

HOME
react-native
loops
scadalts
google-maps-api-3
encryption
composer-php
less
chart.js
conditional
keyboard-shortcuts
crc
ccache
boost-intrusive
junit4
pixel
jquery-gmap3
biopython
markup
accurev
selenium-rc
stackdriver
cdb
ssrs-2016
apache-samza
vowpalwabbit
nodemon
dsolve
uppaal
affiliate
gmail-contextual-gadgets
kernel-module
basic4android
toml
960.gs
binary-image
background-position
safe-browsing
virsh
tivoli-identity-manager
graylog
equation
dnspython
orders
embedded-javascript
erwin
machinekey
munin
outlook-2016
imshow
pubsubhubbub
mockery
deferred-loading
owner
google-plus-domains
nested-forms
reducers
grid-layout
spsite
togglz
hottowel
amdatu
elasticsearch-marvel
gamma-function
determinants
lastaccesstime
ngen
magnetometer
build-system
desktop-shortcut
jekyll-extensions
deface
ntl
raii
intel-galileo
complete.ly
contactscontract
emacs-semantic
mic-1
wsimport
uipicker
facebook-events
payflowlink
file-association
ipad-2
pmp
jlayer
nexus-7
spaces
looper
mindmapping
ant4eclipse
maintenance-plan

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile