Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to get enum from a different namespace.

Reply
Thread Tools

How to get enum from a different namespace.

 
 
werasm
Guest
Posts: n/a
 
      09-22-2006

Jens Theisen wrote:
> Comeau and gcc disagree, so I guess it's nonstandard. What compiler do
> you use?


I think so too.

>
> I don't know of a way to practically forward declare an enum in a
> standard way or a feasible workaround. If someone does, I'd like to
> hear.


I don't think there is one. I don't know why, but think there may be
good reason. Maybe some std guru can comment . The fact that comeau
does not compile it, makes me wonder. I know VCC does compile it, but I
work on more than one compiler - 4 to be precise (5 comeau included).

> An alternative is to wrap it in a namespace:


The problem with wrapping in a namespace, is that each value that forms
part of the enum must be explicitly qualified (I think).

therefore...

using MyEnum::enum_t;

int x = static_cast<int>(eSome); //error, no type eSome
// in scope, required to qualify with MyEnum...

Regards,

Werner

 
Reply With Quote
 
 
 
 
Jens Theisen
Guest
Posts: n/a
 
      09-22-2006
"werasm" <(E-Mail Removed)> writes:

> The problem with wrapping in a namespace, is that each value that forms
> part of the enum must be explicitly qualified (I think).
>
> therefore...
>
> using MyEnum::enum_t;
>
> int x = static_cast<int>(eSome); //error, no type eSome
> // in scope, required to qualify with MyEnum...


Not if you use a using directive:

using namespace MyEnum;
^^

Regards,

Jens
 
Reply With Quote
 
 
 
 
werasm
Guest
Posts: n/a
 
      09-22-2006

Jens Theisen wrote:
> "werasm" <(E-Mail Removed)> writes:


> Not if you use a using directive:
>
> using namespace MyEnum;


Yes, true. Unfortunately this is not a good idea in an .h file (for
qualification in class definitions). In general, it is not common to
use enum values in class definitions, though. If the need was required,
using an anonymous enum with a class is quite handy.
struct someS{
typedef x::z enum_t enum_t;

//now values qualified here by enum_t::value;
};

I have also found that the class name can form part of the actual enum
name (nice feature for categorisation). This could be true for
namespaces too, though.

struct State
{
enum
{
Standby, Maintenance, Operational, Shutdown
};
};

Now qualify like:

State::Standby etc... The qualifier name conveys information about the
category of the enumerated type. A feature we often use. The biggest
reason for doing this though, is the fact that forward declaration of
enums don't work. If that was not the case, I would probably consider
using namespaces to scope my enums like you propose.

Regards,

Werner

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-22-2006

Jens Theisen wrote:
> "mlimber" <(E-Mail Removed)> writes:
> > This is entirely unnecessary since you can forward-declare enums
> > without the struct:

>
> Comeau and gcc disagree, so I guess it's nonstandard. What compiler do
> you use?


How about that? Comeau and gcc do issue a warning, but VC++ 6, 7, 7.1,
8, and (most surprisingly) EDG don't.

> I don't know of a way to practically forward declare an enum in a
> standard way or a feasible workaround. If someone does, I'd like to
> hear.


How about the technique presented here:
http://www.ddj.com/dept/cpp/184403894

Cheers! --M

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments