Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Possible to promote scoped enum values to enclosing namespace?

Reply
Thread Tools

Possible to promote scoped enum values to enclosing namespace?

 
 
Old Wolf
Guest
Posts: n/a
 
      02-04-2013
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).

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      02-04-2013
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.
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      02-06-2013
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

 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      02-06-2013
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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      02-06-2013
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).
 
Reply With Quote
 
Martin Shobe
Guest
Posts: n/a
 
      02-08-2013
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
enum promote to bool type rather than Integer type? FE C++ 6 08-04-2009 03:21 PM
Initial values of File scoped and Block level variables Madhav C Programming 27 01-16-2006 03:29 AM
Initial values of File scoped and Block level variables Madhav C Programming 4 01-08-2006 01:44 PM
scoped enum implementation Jonathan Mcdougall C++ 0 02-13-2005 05:50 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57