0

from the below question i sort of get how enums and namespace scoping works

Scope resolution operator on enums a compiler-specific extension?

However with regard to test code below i'm confused as to why in the below code snippet:

1) i can refer to return type in function signature as test_enum::foo_enum

2) however "using namespace test_enum::foo_enum" is not allowed

namespace  test_enum { 

   enum foo_enum { 

      INVALID, 
       VALID
    };
} 

// Case 1) this is allowed 
test_enum::foo_enum getvalue() {

     return test_enum::INVALID;

}

//Case 2) is not allowed 

using namespace test_enum::foo_enum; 

is there a particular reason for not allowing case 2 ?
Also are "enums" more of C style construct and better to avoid in C++ code ?

1 Answer 1

4

The reason using namespace test_enum::foo_enum; is not allowed is because foo_enum is not a namespace, it is an enum. What works is using test_enum::foo_enum;

I believe what you are trying to do is something like this:

namespace foo_enum {
    enum foo_enum_t {
        INVALID,
        VALID,
    };
}

using foo_enum::foo_enum_t;

This allows you to throw around foo_enum_t freely, but you still have to type out foo_enum::INVALID or foo_enum::VALID

Sign up to request clarification or add additional context in comments.

2 Comments

I agree with the namespace-scoped enum pattern. We have a similar coding guideline in place on my team.
POSIX reserves identifiers ending in _t, so they're best avoided in code intending to be portable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.