0

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)

           ^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Michał Walenciak
  • 4,257
  • 4
  • 33
  • 61

2 Answers2

3

You can write:

case decltype(data.en)::entry:

however I think it would not be considered good code.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

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();
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335