2

According to this link

A point from C++0x draft : n3290

Shall this program is correct ?

EX:
namespace  X {};
enum Foo
  {
      X = 0,     #1
      Y,
      Z = X // X refers to the enum, not the type
  };

Iam getting error while execution this program like // #1 'X' redeclared as different kind of symbol

But in the above statement link ...namespace scopes containing the enum-specifier. ...etc

please clarify my doubt.

Otherwise Please any one give me an example that proves the above statement(in link) with namespace

Community
  • 1
  • 1
user751747
  • 1,129
  • 1
  • 8
  • 17

2 Answers2

1

There is a difference in that the original question had a struct X instead of namespace X. The namespace name is visible in this scope, and so is Foo::X as enum names "leak" into the surrounding namespace. That creates a conflict.

In C (and therefore also in C++) the name of a struct/class/union is in a separate "tag namespace" (a C term with a different meaning) which allows us to declare another item using the same name in the same scope:

Difference between 'struct' and 'typedef struct' in C++?

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • But with Struct X example it won't create an conflict acoording to the above ISO statement .but in terms of an namespace it is not working ... may i know the reason ...why ? please explain it – user751747 Jul 25 '11 at 09:54
  • I thought I did that. :-) With a struct you are allowed to hide the name by declaring another one. You can always get at the struct anyway by writing `struct X`. This is inherited from C. Namespaces are a C++ feature and works differently. – Bo Persson Jul 25 '11 at 09:58
0

The program is illegal. An enum does not introduce a separate scope (unless you are using C++11 and add class to it), both the namespace X and the enumeration constant X are in the same scope. There are only two cases where the same name can be defined more than once in the same scope: overloaded functions, and one class name. The special case for the class name is purely for C compatibility, so that C API's with functions like:

struct stat { ... };
int stat(const char* path, struct stat* buf);

wouldn't break. If both a class name and another name are present, the other name has precedence, unless preceded by a class keyword.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Note, in C++11, you don't need `enum class` for enumerators to have scope. They always have scope with the enumeration name (assuming they have an enumeration name). But, for backwards compatibility, they retain their old scope. So `Y` and `Foo::Y` would both be legal in C++11. – Nicol Bolas Jul 25 '11 at 21:03