"Ian Collins" <ia******@hotmail.comwrote in message
news:5m************@mid.individual.net...
Chris Thomasson wrote:
>"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:%k****************@newssvr17.news.prodigy.ne t...
>>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...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?