Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behaviour between Clang and GCC when performing qualified name lookup

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

like image 652
Supremum Avatar asked Jul 16 '15 21:07

Supremum


People also ask

Does Clang optimize better than GCC?

GCC consistently outperformance Clang on all optimization levels. 32 Bit Performance is on a bit lower side with respect to corresponding 64-bit compilers & optimization levels. This can be attributed to being able to utilize the RAM properly. The contrast between O0 & Other optimization levels is very visible.

Which is faster Clang or GCC?

Sometimes a program is a lot faster when compiled with GCC, sometimes it's a lot faster with clang. Usually it's marginally faster with GCC. Clang attempts to unroll loops really, really aggressively. Even at -O2 : Clang's loop unrolling attempts at -O2 are more aggressive than GCC's loop unrolling attempts at -O3 .

Is Clang compatible with GCC?

GCC and C99 allow an array's size to be determined at run time. This extension is not permitted in standard C++. However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs. If you would prefer not to use this extension, you can disable it with -Werror=vla.

Is Llvm slower than GCC?

While LLVM's Clang C/C++ compiler was traditionally known for its faster build speeds than GCC, in recent releases of GCC the build speeds have improved and in some areas LLVM/Clang has slowed down with further optimization passes and other work added to its growing code-base.


1 Answers

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.

like image 91
Angew is no longer proud of SO Avatar answered Sep 28 '22 19:09

Angew is no longer proud of SO