7

At work we have a long-running project which is compiled successfully with GCC (8.2). When we tried to use clang-based tools (clang-tidy, the static analyzer, etc.), we discovered that clang sometimes reports an error on code accepted by GCC. Here's a recent example:

#include <vector>

struct Fox{};

using Foxes = std::vector<Fox>;

Foxes& foxes()
{
    static Foxes f;
    return f;
}

void foxy(Foxes& foxes = foxes())
{
    (void)foxes;
}

The above is accepted by GCC (both 8.2 and 12.1) with -Wall -Wpedantic -Wextra -Wshadow -Werror. However, clang complains that error: default argument references parameter 'foxes'.

https://godbolt.org/z/seEb1ahKP

It looks like clang considers the foxes to the right of the = sign the same as the foxes to the left of it. GCC appears to consider the foxes to the right of the = to be the function defined earlier.

When GCC and clang disagree, at least one of them is usually wrong (unless the code is ill-formed but no diagnostic required). Which one is right here?

GCC's behavior seems suspect to me; I would have thought that at least a -Wshadow warning should have been emitted for the parameter (a local variable) with the same name as a function defined at global scope.

Stephen Newell
  • 7,330
  • 1
  • 24
  • 28
Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56
  • Have you tried checking this against a more recent gcc? – SoronelHaetir Jul 27 '22 at 21:53
  • 1
    12.1 is the latest GCC release I could find on godbolt.org. But "gcc (trunk)" behaves the same way (accepts the code). – Bulletmagnet Jul 27 '22 at 22:02
  • I linked a duplicate, but there are also multiple as of yet open GCC bug reports about this, e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62244, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50479, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84373. It is probably not considered high priority since it only allows for otherwise invalid code to compile. – user17732522 Jul 27 '22 at 22:50

0 Answers0