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

Tell GCC “I want to use SSE4.1, but ONLY in the places I want!”
My Code of C++ is not Working
Dividing large double by large int
C++ drawing map accordingly to the speed
Using deduced types in other function templates, argument
Trouble with Polygon in Stroustrup's PPP book
Is there a case missing in §8.5 p17 (semantics of initializers) of N3797?
Migrating from Boost to the Standard Library for C++11
Perfect-forwaring of the variadic template parameters of a struct
Force Program to Start As Root and Drop it later
Is the memory initialization/deletion so time-consuming?
Debugging CUDA - CudaUnknownError
Simple decoding program C++ [closed]
Qt Application Slowing Down
How to Set a Class Instance Name to a Variable (C++)
DirectX 11 Make a square.

Categories

HOME
okta
lucene
powershell-v4.0
asynchronous
kotlin
post
windows-forms-designer
google-cloud-endpoints
geogebra
chat
uinavigationcontroller
codeception
mupdf
affinity
libreoffice-base
markup
4d-database
qtspim
entity-relationship
pex
revit
indexoutofrangeexception
eof
ractivejs
indexof
messagebox
vowpalwabbit
android-ffmpeg
neoscms
javabeans
street-address
nestedscrollview
javapoet
database-theory
bootstrapping
scada
android-runonuithread
redhat-bpm
lift
bspline
3d-modelling
model-driven
scalacheck
parasoft
auto-value
google-truth
winlims
bandwidth
sp-executesql
django-compressor
sift
shodan
siteminder
stdio
rpath
report-viewer2012
plone-4.x
devpartner
ienumerable
rc4-cipher
amazon-mobile-analytics
unreal-blueprint
highcharts-ng
divider
supercsv
xcode-bots
kermit
savefiledialog
maven-site-plugin
google-text-to-speech
wpftoolkit
jquery-dynatree
filefield
pre-commit-hook
pixate
restriction
bonjour
freetextbox
buttonclick
offlineapps
texmacs
google-email-settings-api
stencils
scite
divshot
cvxopt
django-staticfiles
server-side-scripting
mysql-5.0
icefaces-1.8
ejbql
memory-mapped-files
shadowbox
osx-gatekeeper
launching-application
statusnet
django-generic-views
google-instant-previews
tomahawk
blogml
orbited
habari
resource-cleanup
release-builds

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