Enums

J

Jon Slaughter

I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

is equivalent to

static const int sun = 0;
static const int mon = 1;

but I'd like not to have these global.

I can do day2::sun and use that but I get a warning

about nonstandard extension. It does work though.

I suppose I can wrap it in a static class and use it... maybe something like

static struct day2
{
static const int sun = 0;
static const int mon = 1;
}


Which seems to work but I get errors about no variables in day2. I suppose I
can fix this in some way but I wondering if there is some other better
method. (using the static constant int every time is a little trouble if its
not necessary)

Any ideas?

Thanks,
Jon
 
J

Jon Slaughter

Actualy what I'm doing is

class X
{
enum Y
{
};
};

and it seems to work fine.
 
A

Alf P. Steinbach

* Jon Slaughter:
I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

is equivalent to

static const int sun = 0;
static const int mon = 1;

Well, it isn't. The enum is a distinct type. And its underlying
integral type need not be int.

but I'd like not to have these global.

I can do day2::sun and use that but I get a warning
about nonstandard extension. It does work though.

Right, that's non-standard.

I suppose I can wrap it in a static class

There's no such thing as static class.

and use it... maybe something like

static struct day2
{
static const int sun = 0;
static const int mon = 1;
}

Missing variable name and missing semicolon. By writing static you
force the compiler to assume that you're declaring a variable. It's OK
to define the struct inline there, but you then need a variable name at
the end, and anyway you need a semicolon.

Which seems to work but I get errors about no variables in day2.

How can it seem to work when it doesn't compile?

I suppose I
can fix this in some way but I wondering if there is some other better
method. (using the static constant int every time is a little trouble if its
not necessary)

Any ideas?

You can wrap the enum in a class or in a namespace.

A class has the advantage that it can be inherited.

A namespace has the advantage that you can use "using".


Cheers, & hth.,

- Alf
 
I

Ian Collins

Jon said:
I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.
but I'd like not to have these global.

I can do day2::sun and use that but I get a warning
You could put the enum in a namespace.

namespace day2
{
enum { sun, mon };
}
 
J

Jon Slaughter

Alf P. Steinbach said:
* Jon Slaughter:

Well, it isn't. The enum is a distinct type. And its underlying integral
type need not be int.

This is what microsoft said, not me.
Right, that's non-standard.



There's no such thing as static class.



Missing variable name and missing semicolon. By writing static you force
the compiler to assume that you're declaring a variable. It's OK to
define the struct inline there, but you then need a variable name at the
end, and anyway you need a semicolon.

I left the semicolon off on purpose just to bother you. jesus christ man!
How can it seem to work when it doesn't compile?



You can wrap the enum in a class or in a namespace.

A class has the advantage that it can be inherited.

A namespace has the advantage that you can use "using".

I suppose a namespace is a good idea. I used a class already though since
that worked for my purposes. I suppose the usage syntax is identical though.

Thanks,
Jon
 
J

Jon Slaughter

Ian Collins said:
Drop that last comma.

god damn people!! First off I cut and pasted this directly from microsofts
help so bitch at them about irrelevant(for my problem) stuff.

http://msdn2.microsoft.com/en-us/library/a6cskb49(VS.80).aspx

You could put the enum in a namespace.

namespace day2
{
enum { sun, mon };
}

Yes, that is a better method I suppose. I wrapped it with a class but I
guess I'll change it to a namescape since it seems better(even though its
probably equivalent).

Thanks,
Jon
 
I

Ian Collins

Jon said:
god damn people!! First off I cut and pasted this directly from microsofts
help so bitch at them about irrelevant(for my problem) stuff.
If you post something that appears incorrect, some one will point his out.
 
I

Ian Collins

Pete said:
The comma is allowed, and sometimes useful.
I didn't think it was, from 7.2 I thought an enumerator-list couldn't
end in a comma, so I just tried to compile the OP's code;

Sun CC says:

Warning: Identifier expected instead of "}".

gcc says:

error: comma at end of enumerator list

Which sums up my interpretation of 7.2.
 
J

Jon Slaughter

Ian Collins said:
If you post something that appears incorrect, some one will point his out.

First off is not necessarily an error.

Second, my question wasn't about a syntax error and the problem has nothing
to do with it. Now if it was a serious issue then I could see the need but
else its just pedantism. I'm sure you have better things to do with your
time than waste it writing such irrelevant things? (of course the 2nd half
of your post was very helpful)

Suppose it was an syntax error. Do you really think it has anything to do
with my original problem? And if it did could it not be easily caught when
the example was tried? This is not some subtle issue that needs
addressing... I know you know that but when you post such things it makes
you seem like you are simple minded are a pedant. Of course maybe you are a
pedant and proud of it? ;)
 
I

Ian Collins

Chris said:
The extra comma is allowed in C99:
Where it is listed as a major change from C95, so probably a change from
C++. I assume the change will carry forward to the next C++ standard?
 
J

James Kanze

Drop that last comma.
[/QUOTE]
The comma is allowed, and sometimes useful.

Not according to ISO 14882:1998. Nor was it allowed in C90. It
is allowed in C99, and in the current draft. I don't know what
the status is in ISO 14882:2003, but it really doesn't matter,
since most compilers haven't even fully implemented C++98 yet.
G++ doesn't allow it, for example (and only allows -std=c++98,
not -std=c++03).

Of course, the fact that C90 didn't allow it was probably an
oversight (copied by C++98); I suspect that many compiler
writers considered it as such, and didn't forbid it. But as
long as some do, you're stuck with dropping it, at least in
portable code.
 
C

Chris Thomasson

Ian Collins said:
Chris said:
Jon Slaughter said:
I'm declaring an enum and I would like to limit the scope
[...]

There is a possible solution in the form of a macro hack:

http://appcore.home.comcast.net/misc/enum-scope-cpp.html
That's better than using a namespace?

Well, not really; those macros were kind of on the hacky/crazy side...

;^)


Anyway, I think I agree with the "apparent" line of *reasoning I deciphered
from your post. Basically, use the namespace keyword to confine an enum
within its scope. Unfortunately, there could be a problem with this approach
because you can't use a namespace within the scope of a struct/class:

____________________

class/struct {
namespace xxx {} // syntax error.
class/struct yyy {} // okay.
};
____________________


Right? I am not an "expert" in the C++ Standard. Therefore, I am probably
missing something real simple here...




*-- Here is some quick example code that tries to fit the bill:

____________________

namespace foolib {
namespace constant {
namespace config {
enum value_e {
value_a,
value_b
};
}} // namespace constant::config


struct foo {
#undef MYCONST
#define MYCONST foolib::constant
struct constant {
struct state {
enum value_e {
value_a = MYCONST::config::value_a + 1,
value_b = MYCONST::config::value_b + 1
};
};}; // struct constant::state
}; // class/struct foo


namespace constant {
namespace state {
#undef MYCONST
#define MYCONST foolib::foo::constant
enum value_e {
value_a = MYCONST::state::value_a - 1,
value_b = MYCONST::state::value_b - 1
};
}} // namespace constant::state

} // namespace foolib



#include <assert.h>
#include <stdio.h>

int main(void) {
namespace myconst = foolib::constant;

assert(
(myconst::config::value_a ==
myconst::state::value_a) &&

(myconst::config::value_b ==
myconst::state::value_b)
);

using namespace std;

puts("done; hit <enter>");
getchar();

return 0;
}

____________________


Something like that looks better than the contrived macro-based stuff no?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top