How to write a 'using' statement for enum classes?



In a rock, paper, scissors program that I am writing, I am enumerating the three different moves and declaring them as a class. However, when I try to write a using statement so that I have to avoid using the scope operator, it doesn't seem to work. Anyone know why?

enum class choice {rock, paper, scissors};

using namespace choice;

Here an error message comes up, saying: [Error] 'choice' is not a namespace name. Why is this? I thought that for choice could be a namespace, in this context.

5 Answers: 

The behaviour you want can be achieved with namespace choice { enum choice { ... }; }. It will work only for values though, you still have to use choice::choice if you want to declare a variable. Unless you also use auto, of course.

choice isn't a namespace, so using namespace choice; isn't valid. If you don't want to have to use a scope designator, don't use a scoped enum. A plain enum will work for what you've mentioned so far:

enum choice { rock, paper, scissors };

enum class choice {rock, paper, scissors}
rock = choice::rock, paper = choice::paper, scissors = choice::scissors;

If like below, I just use "typedef A::B::C C".

namespace A
    class B
        enum class C : unsigned char


namespace choice
    enum class type {rock, paper, scissors};
    constexpr auto rock     = type::rock    ;
    constexpr auto paper    = type::paper   ;
    constexpr auto scissors = type::scissors;

int main()
    choice::type move;
    using namespace choice;
    move = rock;
    move = paper;
    move = scissors;

    return 0;