Possible to promote scoped enum values to enclosing namespace?

O

Old Wolf

enum class Foo
{
fooBar
};

int main()
{
Foo::fooBar; // ok
fooBar; // error
}


Is there any directive I can issue that will make the line marked
"error"
correct - i.e. make the members of the enum valid identifiers in the
current namespace?

The reason is that the enum definition is in an auto-generated header,
and the auto-generating program has been updated to now use
scoped enums where previously it used plain enums. I would like
to avoid having to add the prefix to every instance that an enum value
is
used in the codebase (and also preferably not change the
auto-generated code, as it would mean reapplying the change
each time the code is refreshed).
 
Ö

Öö Tiib

enum class Foo
{
fooBar
};

int main()
{
Foo::fooBar; // ok
fooBar; // error
}


Is there any directive I can issue that will make the line marked
"error"
correct - i.e. make the members of the enum valid identifiers in the
current namespace?
No.

The reason is that the enum definition is in an auto-generated header,
and the auto-generating program has been updated to now use
scoped enums where previously it used plain enums.

So ... you had something like?:

enum Animal
{
Animal_Dog,
Animal_Frog,
Animal_Donkey
};

Then that got refactored into?:

enum class Animal
{
Animal_Dog,
Animal_Frog,
Animal_Donkey
};

That is yes ... annoying double prefixes.
I would like to avoid having to add the prefix to every instance
that an enum value is used in the codebase (and also preferably
not change the auto-generated code, as it would mean reapplying
the change each time the code is refreshed).

You should change the generator to generate less prefixes:

enum class Animal
{
Dog,
Frog,
Donkey
};

Then the code likely stops being annoying stutter.
 
O

Old Wolf

enum class Animal
{
Animal_Dog,
Animal_Frog,
Animal_Donkey
};

That is yes ... annoying double prefixes.

Yeah that's basically it. I can't change the generator unfortunately, I guess I will just have to do the mass copy-paste.

Or stick with my temporary solution of
#define Animal_Dog Animal::Animal_Dog
// ad nauseum
 
G

Gerhard Fiedler

Old said:
Or stick with my temporary solution of
#define Animal_Dog Animal::Animal_Dog
// ad nauseum

If the "ad nauseum" part is really nauseatingly long, there's Boost
macro programming that may make this shorter (possibly not less
nauseating, though :)

Gerhard
 
Ö

Öö Tiib

Yeah that's basically it. I can't change the generator unfortunately,
I guess I will just have to do the mass copy-paste.

If generator is not yours then fire a defect report? It is always with
every new thing ... idiots immediately squeeze the worst out of it.
Or stick with my temporary solution of

#define Animal_Dog Animal::Animal_Dog
// ad nauseum

Someone may become confused by such macros and then its yours fault.
Maybe try something like:

#define CREATURE(X) Animal::Animal_##X

Then you can use CREATURE(Dog), CREATURE(Frog), CREATURE(Donkey).
 
M

Martin Shobe

Yeah that's basically it. I can't change the generator unfortunately, I guess I will just have to do the mass copy-paste.

Or stick with my temporary solution of
#define Animal_Dog Animal::Animal_Dog
// ad nauseum

Or possibly

constexpr auto Animal_Dog(Animal::Animal_Dog);


Martin Shobe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top