3

My question is simple, why a class and its nested class cannot have same name, but namespaces can?

for example:

class Test {
    class Test {};
};

is invalid, but

namespace Test {
    namespace Test {}
}

is valid

  • Namespaces don't offer *constructors*. The name `Test` within class `Test` is reserved for that specific use. – WhozCraig Aug 01 '20 at 13:57
  • `class Test { struct Wrapper { class Test {}; }; };` should work. Just need a dummy wrapper between the outer class and and same-named inner class. – Eljay Aug 01 '20 at 14:05
  • @Eljay Could you explain how does the dummy work in linker? –  Aug 01 '20 at 14:10
  • The dummy Wrapper acts as-if it were a namespace. (Before namespaces were in C++, some people used structs for that purpose.) The linker doesn't care, it just uses the names, and C++ mangles the names (which is implementation specific) to something that could be `$Test` and `$Test$Wrapper$Test`. – Eljay Aug 01 '20 at 14:14
  • So why don't compiler just generate different names for those constructors, Were C++'s designer predicted that? –  Aug 01 '20 at 14:18
  • 1
    Because it would be ambiguous, hence the C++ programmer is responsible for making the node not ambiguous. – Eljay Aug 01 '20 at 14:44

1 Answers1

2

Class X already has a member named X, referring to itself. This is known as an injected class name. It's then invalid to add another member with the same name.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • So outer in both local and global scope(the order of global is lower), and outer and inner crash in local scope? –  Aug 01 '20 at 14:34
  • I do not understand this question, sorry. – Igor Tandetnik Aug 01 '20 at 14:34
  • And look this https://stackoverflow.com/questions/5856759/can-a-class-share-a-namespaces-name , the comment under the answer says compiler need disambiguate problem of type first, so why the order of variable is higher? –  Aug 01 '20 at 14:38
  • I'm still not sure what you are asking, nor how this question relates to your example. – Igor Tandetnik Aug 01 '20 at 14:40
  • Don't worry about the first question, just look the last question –  Aug 01 '20 at 14:41
  • There are no variables in the code you've shown. What do you mean by "order", and what do you believe is higher than what else? – Igor Tandetnik Aug 01 '20 at 14:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219046/discussion-between-alan-jian-and-igor-tandetnik). –  Aug 01 '20 at 14:43
  • And look this https://stackoverflow.com/questions/16329962/why-can-in-class-initializers-only-use-or, the first answer shows the compiler don't know which Overloaded are we talking. –  Aug 01 '20 at 14:49
  • If you actually [compile the code](https://godbolt.org/z/nossfz) shown in that answer, you'll see `error: 'Overloaded' is not a type`. Which clearly indicates that the compiler has resolved the name `Overloaded` to the `int` variable and not to the type name. – Igor Tandetnik Aug 01 '20 at 14:56
  • So the conclusion is there are two struct X in same local scope? –  Aug 01 '20 at 15:11
  • Well, there would have been, had your example compiled successfully. But that's not allowed, and so your example fails to compile. – Igor Tandetnik Aug 01 '20 at 15:14