Archived

This topic is now archived and is closed to further replies.

PlayGGY

Enum namespaces in C++.

Recommended Posts

OK, I think I know the answer to my problem, and it is "no", but I'll see if I am wrong. In C++ enums don't get their own namespace, so you can easily have name collisions. Is there a cleaner way of doing than this, for example:
namespace Day{
   enum Day{Monday, Tuesday};
}  
That puts it in its own namespace, but then whenever I want to make a Day, I have to say "Day::Day". Is there a way to make only the constants in their own namespace, while still using enums? [edited by - PlayGGY on March 17, 2004 7:43:22 PM]

Share this post


Link to post
Share on other sites
If you used:

using namespace Day; 


before the code where you''re creating your Day object, you would not have to refer to it as Day::Day.

- Nanoprobe

Share this post


Link to post
Share on other sites
And that would defeat the whole purpose of putting it in the namespace to begin with. I'm not sure, but you could try "using enum Day::Day;" or something. Just a guess.

[edited by - Doc on March 17, 2004 8:14:46 PM]

Share this post


Link to post
Share on other sites
That reminds me *looks up MSDN*:

using Day::Day;  


That *should* let you use "Day" and still not constantly be in the Day namespace.

Doc - yeah, it kinda does defeat the purpose if you're doing something small.

[edit]using enum Day::Day as Doc said might also work, I'm having a hard time reading what MSDN says on it...

[edited by - Nanoprobe on March 17, 2004 8:26:05 PM]

Share this post


Link to post
Share on other sites
Thanks for the help, but since I am trying to make it as user-friendly as possible, I think I will do it my ugly way.

Enums should have their own namespace automatically.

[edited by - PlayGGY on March 17, 2004 8:38:05 PM]

Share this post


Link to post
Share on other sites
How about:

class enum_name {
public:
enum internal_type {
ALPHA,
BETA
};
private:
internal_type value;
public:
operator internal_type() const {
return value;
}

bool operator==(internal_type ovalue) const {
return value == ovalue;
}
bool operator!=(internal_type ovalue) const {
return value != ovalue;
}

enum_name() {
}
enum_name(internal_type ovalue) : value(ovalue) {
}
};

I''ve written a couple macros that automate the whole thing for me.

Share this post


Link to post
Share on other sites
They pretty much buggered this one up from the start, didn''t they? Blame C.

Share this post


Link to post
Share on other sites
Hey, that is clever!

But it is pretty sad that you need a hack to do something that should be built in to the language.

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
How about:
That''s what I thought initially, but is this really going to behave like an enum? Even though there are conversions both ways, it seems like somewhere in a dark corner of the Standard, there ought to be a rule that will break your construction.

Anyway, if using Day::Day works, that''s the perfect solution IMO.

Share this post


Link to post
Share on other sites
quote:
Original post by Cedric
That''s what I thought initially, but is this really going to behave like an enum? Even though there are conversions both ways, it seems like somewhere in a dark corner of the Standard, there ought to be a rule that will break your construction.

It seems to work for me. I have a project filled with such hackish namespaced-enumerations (and something similar for namespaced-bit-masks) and haven''t had any problems.

Share this post


Link to post
Share on other sites