Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto template parameters: g++ 7.3 vs clang++ 6.0 : Which compiler is correct?

Two compilers produce different results for this code example. Clang generates two different types. G++ uses same type for fu and fi. Which one is standard compliant?

#include <iostream>

template< auto IVAL>
struct foo {
    decltype(IVAL) x = -IVAL;
};

int main()
{
    foo<10u> fu;
    foo<10> fi;
    std::cout << fi.x << " " << fu.x << '\n';
    return 0;
}

g++-7.3 output:

4294967286 4294967286

clang-6.0 output:

-10 4294967286

like image 258
random Avatar asked Feb 03 '18 06:02

random


People also ask

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 .

Does clang optimize better than GCC?

Clang reduces the single-thread compilation time by 5% to 10% compared with GCC. Therefore, Clang offers more advantages for the construction of large projects.

Is G ++ and GCC the same?

DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.


1 Answers

gcc is wrong here, these are clearly two distinct types.

And to confirm - this bug is fixed in gcc 8.0.1

Sample code

like image 131
berkus Avatar answered Sep 21 '22 18:09

berkus