Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Possible to promote scoped enum values to enclosing namespace? (http://www.velocityreviews.com/forums/t957245-possible-to-promote-scoped-enum-values-to-enclosing-namespace.html)

Old Wolf 02-04-2013 01:21 AM

Possible to promote scoped enum values to enclosing namespace?
 
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 02-04-2013 09:38 AM

Re: Possible to promote scoped enum values to enclosing namespace?
 
On Monday, 4 February 2013 03:21:00 UTC+2, Old Wolf wrote:
> 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.

Old Wolf 02-06-2013 12:17 AM

Re: Possible to promote scoped enum values to enclosing namespace?
 
On Monday, February 4, 2013 10:38:00 PM UTC+13, Öö Tiib wrote:
> 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


Gerhard Fiedler 02-06-2013 01:43 PM

Re: Possible to promote scoped enum values to enclosing namespace?
 
Old Wolf wrote:

> 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 02-06-2013 08:07 PM

Re: Possible to promote scoped enum values to enclosing namespace?
 
On Wednesday, 6 February 2013 02:17:39 UTC+2, Old Wolf wrote:
> On Monday, February 4, 2013 10:38:00 PM UTC+13, Öö Tiib wrote:
> > 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.


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).

Martin Shobe 02-08-2013 04:05 PM

Re: Possible to promote scoped enum values to enclosing namespace?
 
On 2/5/2013 6:17 PM, Old Wolf wrote:
> On Monday, February 4, 2013 10:38:00 PM UTC+13, Öö Tiib wrote:
>> 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
>


Or possibly

constexpr auto Animal_Dog(Animal::Animal_Dog);


Martin Shobe



All times are GMT. The time now is 10:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.