9

The following code compiles with all 3 major compilers without any problems (MSVC has a bug with accepting of invalid similar code, but that is unrelated to my question):

#include <iostream>

namespace Z{
    struct Na{
        struct Batman{
            int x=47;
        };
    };
}

int main(){
   using Na = Z::Na;
   return Na::Na::Na::Na::Na::Na::Na::Batman{}.x;
}

Is there any logic behind this, or is it just a weird edge case that ends up being legal when applying rules made for "normal" uses?

If my confusion is not clear: I would expect the weird line to be parsed either as

  • many nested namespaces, e.g. std::std::std and rejected
  • a reference to a constructor (Na::Na) and then rejected when the following ::Na is encountered
Boann
  • 48,794
  • 16
  • 117
  • 146
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • 7
    I want to upvote a second time for the example code :) Though `x` should be 42 ;) – cigien Nov 30 '20 at 17:03
  • 1
    fwiw, I never saw Batman and any of those three compilers in the same room at the same time – 463035818_is_not_an_ai Nov 30 '20 at 17:06
  • 3
    @Downvoter In what way can this question be improved? – Bathsheba Nov 30 '20 at 17:21
  • 2
    @Bathsheba It's unfortunate for sure. fwiw I'm pretty sure this was down-voted *after* the question was hammered. Maybe someone DVed because of lack of research, but how the OP would have found the target without knowing the specific term is beyond me. – cigien Nov 30 '20 at 17:25
  • 2
    I myself would have gone for a struct field named `HeyHey` but maybe I'm from a different era of pop culture ... – davidbak Nov 30 '20 at 17:27
  • 3
    @Bathsheba That would mean that no one could ask [tag:language-lawyer] questions any more, so maybe not a terrible idea ... ;) – cigien Nov 30 '20 at 17:31
  • holy injected class name Batman! – JHBonarius Nov 30 '20 at 17:49

0 Answers0