The Usage of Anonymous Enums

The usage of anonymous enums

Enums don't take up any space and are immutable.

If you used const int color = 1; then you would solve the mutability issue but if someone took the address of color (const int* p = &color;) then space for it would have to be allocated. This may not be a big deal but unless you explicitly want people to be able to take the address of color you might as well prevent it.

Also when declaring a constant field in a class then it will have to be static const (not true for modern C++) and not all compilers support inline initialization of static const members.


Disclaimer: This answer should not be taken as advice to use enum for all numeric constants. You should do what you (or your co-workers) think is more readable. The answer just lists some reasons one might prefer to use an enum.

in which situations anonymous enum should be used?

In C (but not in C++), enum can be [ab]used to define int constants.

For example, given this declaration:

const int MAX = 1024;

MAX is not a constant expression, it's the name of a read-only object. That means you can't use it in a case label, as the size of an array declared at file scope or with static, or in any other context requiring a constant expression.

But if you write:

enum { MAX = 1024 };

then MAX is a constant expression of type int, usable in any context where you could use the constant 1024.

Of course you could also write:

#define MAX 1024

but there are disadvantages to using the preprocessor: the identifier isn't scoped the way it would be given an ordinary declaration, for example.

The drawback is that such a constant can only be of type int.

C++ has different rules; enumeration constants are of the enumerated type, not int, but you can use declared constant objects as constant expressions (as long as the initializer is a constant expression).

To address the original question, when you use an enum declaration to create constants like this, there's no point in having either a tag or a typedef, since you'll never use the type itself.

Background: This:

enum foo { zero, one, two };
enum foo obj = two;

creates a type enum foo and constants zero, one, and two. In C, the constants are always of type int, which is admittedly odd, and the initialization of obj involves an implicit conversion from int to enum foo.

In C++, the type enum foo can also be referred to as just foo, and the constants are of type enum foo (which is compatible with some integer type, not necessarily int).

Java: anonymous enums?

No Java does not support anonymous enums.

Just declare them inside the class with no external visibility:

public class Test {
private static enum Option {
FirstOption,
SecondOption,
ThirdOption
}

Option option;
}

Is there a way to declare a function argument to take an anonymous enum?

As others have suggested, you can simply use an int in the place of ???.

This is because as per 6.7.2.2/3 of C11 standard (Committee draft):

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.

How to declare function with anonymous enum in function signature?

You cant do that, obviously

Standard C11

6.2.4 Storage durations of objects


  1. An enumeration comprises a set of named integer constant values. Each distinct
    enumeration constitutes a different enumerated type.

but you can use named one

#include <stdio.h>

enum my_enum
{
a,
b,
c,
MY_ENUM_MAX
};

void func(enum my_enum value)
{
printf("%d\n", value);
}

int main(void)
{
func(a);
func(b);
}

or you can typedef it

#include <stdio.h>

typedef enum
{
a,
b,
c,
MY_ENUM_MAX
}my_enum;

void func (my_enum value)
{
printf("%d\n", value);
}

int main(void)
{
func(a);
func(b);
}

(JAVA Enums) - Anonymous class inside enum constant

You could do that, it is a perfectly valid solution.

As a recommendation, make your enum implement your interface to make the code more readable:

public enum PotatoEnum implements IPotato{

I_WANT_TO_EAT_POTATO(){

@Override
public void eatPotato() {
// Cant put code here.

}},//more ENUMS ;

}

Anonymous enum classes

No, fruitType is a variable (despite Type in the name). You cannot use a variable name to access things about its type.

The idea with enum class is that the values are not visible outside the definition unless you prefix them with the type name. If the type doesn't have a name, this will be difficult!

How to use anonymous unions with enums?

If I understand your question correctly, this should work:

struct A
{
struct B {
enum { X,Y,Z} foo;
int x;
};
struct C {
enum { M,N,O} bar;
double m;
};
union {
B b;
C c;
};
};


Related Topics



Leave a reply



Submit