开发者

Help ,clarify my doubt on Unqualified Name Lookup :ISO n3290 Draft

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 er开发者_JAVA百科ror 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


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++?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜