9

Consider the following program:

#include <iostream>

namespace N {
    int j = 1;
}

namespace M {
    typedef int N;
    void f() {
        std::cout << N::j << std::endl;
    }
}

int main() { M::f(); }

Compiling it with clang gives the following compiler error:

prog.cc:10:22: error: 'N' (aka 'int') is not a class, namespace, or
enumeration
    std::cout << N::j << std::endl;
                 ^ 1 error generated.

GCC does not give any compiler error. I'm trying to figure out for what compiler I should file the bug report for. Which compiler has the correct behaviour and why (references to the c++ standard)?

Wandbox - Clang: http://melpon.org/wandbox/permlink/s0hKOxCFPgq5aSmJ

Wandbox - GCC: http://melpon.org/wandbox/permlink/i2kOl3qTBVUcJVbZ

Supremum
  • 542
  • 7
  • 23

1 Answers1

11

Clang is correct on this one. Quoting C++11, 3.4.3/1 [basic.lookup.qual]:

... If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types. If the name found does not designate a namespace or a class, enumeration, or dependent type, the program is ill-formed.

Per this clause, types are supposed to be considered during the lookup, so the typedef N should be found. And since it does not designate a namespace, class, enumeration, or dependent type, the program is ill-formed.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • But isn't "Ill Formed" programs a bit like "undefined behaviour", that it's perfectly valid for the compiler to a) not give an error and b) produce code that works in some way [including correctly or making deamons fly out your nose] (Not saying a big shouldn't be raised, just that it's not, technically, wrong to have the compiler NOT produce an error] – Mats Petersson Jul 16 '15 at 21:37
  • 4
    @MatsPetersson The standard implies a required diagnostic when it says "ill-formed". It will typically say "the program is ill-formed, no diagnostic required" when a diagnostic is not required, and "the behavior is undefined" in undefined cases. – David G Jul 16 '15 at 21:43
  • 1
    @Mats Petersson: No, unless "no diagnostics required" is mentioned for a case of ill-formed program the compiler has to produce at least one diagnostic message. – Supremum Jul 16 '15 at 21:44