Possible to promote scoped enum values to enclosing namespace?

Discussion in 'C++' started by Old Wolf, Feb 4, 2013.

  1. Old Wolf

    Old Wolf Guest

    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).
    Old Wolf, Feb 4, 2013
    #1
    1. Advertising

  2. Old Wolf

    Öö Tiib Guest

    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.
    Öö Tiib, Feb 4, 2013
    #2
    1. Advertising

  3. Old Wolf

    Old Wolf Guest

    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
    Old Wolf, Feb 6, 2013
    #3
  4. 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
    Gerhard Fiedler, Feb 6, 2013
    #4
  5. Old Wolf

    Öö Tiib Guest

    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).
    Öö Tiib, Feb 6, 2013
    #5
  6. Old Wolf

    Martin Shobe Guest

    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
    Martin Shobe, Feb 8, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Mcdougall

    scoped enum implementation

    Jonathan Mcdougall, Feb 13, 2005, in forum: C++
    Replies:
    0
    Views:
    493
    Jonathan Mcdougall
    Feb 13, 2005
  2. mrhicks
    Replies:
    2
    Views:
    391
    Dave Thompson
    Jun 10, 2004
  3. FE
    Replies:
    6
    Views:
    495
    Balog Pal
    Aug 4, 2009
  4. Replies:
    21
    Views:
    522
    Luca Risolia
    Jun 13, 2013
  5. Replies:
    3
    Views:
    263
Loading...

Share This Page