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

Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”
When is the correct time to call restoreState for QHeaderView?
How does it pass lvalue reference to std::forward with CRTP?
Using lower_bound function in loop is giving runtime error?
Problems with rand(), always taking the same random generated number [duplicate]
Reading Credentials file fails with error 32
The mechanism of shared_ptr
C++ specialized function template
Compiler errors with cmath on Mac OS X
Array of fstream in C++
Error: (E107) bind interface to port failed: interface already bound to port: port 'MC8051_ALU().AM.port_27' (sc_in)
Error LINK2019 when I try to make a training for face detection [duplicate]
Quickest way to iterate in a C++ vector

Categories

HOME
scadalts
drools
ignite
sharppcap
maven-lifecycle
ssd
contiki
android-gradle
ninja
alexa-voice-service
photon
spring-data-elasticsearch
sprite
derivative
google-nativeclient
google-cloud-vision
face-api
angle
navigation-drawer
u2
povray
byte
rcp
gulp-4
uicollectionviewcell
meta-tags
shieldui
bspline
grayscale
ranorex
enterprise-library-6
mosync
orika
jedi-vim
dependency-properties
same-origin-policy
maven-resources-plugin
erlang-shell
powerview
cpio
aide-ide
z-order
display-bugs
gethostbyname
little-endian
customdialog
iterm
couchbase-sync-gateway
rebase
pdl
wordpress-4.5
bpopup
tap
ngen
rot13
gliffy
ember-testing
ransac
hasp
firstdata
policies
updatemodel
dbase
graphml
maskedinput
outlook-2003
payflowlink
ldapconnection
jpreloader
enctype
nsinteger
sts-securitytokenservice
dynamic-content
registrar
spaces
google-instant-previews
entityreference
white-box-testing
abbreviation
release-builds

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile