Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do gcc and clang generate different symbol names for an instantiation of a function template?

Consider the following example:

struct A {
  using type = int;
};

template <typename T>
using B = A;

template <typename T>
typename B<T>::type f() { return {}; }

template B<int>::type f<int>();

Clang generates symbol named int f<int>() while GCC generates B::type f<int>() for the instantiation: https://godbolt.org/z/MCCza4

Why don't the compilers agree and shouldn't GCC also resolve B::type to int?

like image 901
vitaut Avatar asked Jul 07 '20 03:07

vitaut


People also ask

What is the difference between GCC and Clang?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

Why does Clang use GCC?

Clang is designed to provide a frontend compiler that can replace GCC. Apple Inc. (including NeXT later) has been using GCC as the official compiler. GCC has always performed well as a standard compiler in the open source community.

Are GCC and Clang interchangeable?

TL;DR: Clang is highly compatible to GCC - just give it a go. In most cases, Clang could be used as a GCC drop in replacement ( clang and clang++ are "GCC compatible drivers").


1 Answers

This is a known C++ CWG (Core Working Group) issue: https://wg21.cmeerw.net/cwg/issue2037, quoting Richard Smith:

On the one hand, the alias template can introduce SFINAE conditions, so it should be instantiation-dependent and mangled. On the other hand, the language rules permit this template to be redeclared using the result of expanding the alias template, so mangling it can't be correct.

like image 192
vitaut Avatar answered Sep 28 '22 02:09

vitaut