I recently received a code that is accepted by clang++ but not by g++ and I would like to know which one is right.
The minimalist code reproducing the behavior is very short and talk by itself so I think an explanation would be unnecessarily complicated.
Here is a header containing an extern pointer declaration :
//(guards removed for simplicity) :
#include <type_traits>
using ptr_func = std::add_pointer<void()>::type;
extern ptr_func pointer;
And here is the source implementing the needed pointed function :
#include "function.hh"
void foo() {}
auto pointer = &foo;
The error generated by gcc is as follows :
g++ -c function.cc -std=c++14
function.cc:5:6: error: conflicting declaration ‘auto pointer’
auto pointer = &foo;
^
In file included from function.cc:1:0:
function.hh:5:17: note: previous declaration as ‘void (* pointer)()’
extern ptr_func pointer;
^
Clang accepts this code without any error/warning. And replacing the pointer definition by :
decltype(foo)* pointer = &foo;
is accepted by gcc.
In my opinion, clang is right, but I am not sure so I would like to know if clang is too permissive or if gcc should accept it.