https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106893
Bug ID: 106893
Summary: auto deduces wrong type for function pointer
Product: gcc
Version: 12.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: dan at stahlke dot org
Target Milestone: ---
This works in 11.3.0 but not in 12.2.0.
struct IntegerCoordinate {
int x() const;
int y() const;
};
template <typename T>
struct CoordTraits
{
static auto GetX(T const &p) { return p.x(); }
static auto GetY(T const &p) { return p.y(); }
using Ordinate = decltype(GetX(T{}));
};
template <typename Traits>
struct Node {
static constexpr auto GetX = Traits::GetX; // works because of the `using
Ordinate = ...`
static constexpr auto GetY = Traits::GetY; // compilation error
// This compiles
//static constexpr auto GetX = &Traits::GetX;
//static constexpr auto GetY = &Traits::GetY;
// This compiles
//using fptr = int(*)(IntegerCoordinate const &);
//static constexpr fptr GetX = Traits::GetX;
//static constexpr fptr GetY = Traits::GetY;
// Not needed for minimal test case
auto x(auto const &p) const { return GetX(p); }
auto y(auto const &p) const { return GetY(p); }
};
int main() {
Node<CoordTraits<IntegerCoordinate>> node;
// Not needed for minimal test case
node.x(IntegerCoordinate{});
node.y(IntegerCoordinate{});
}
$ g++ -std=c++20 gcc-function-ptr-bug.cpp -c -o gcc-function-ptr-bug.o
gcc-function-ptr-bug.cpp: In instantiation of ‘constexpr auto (* const
Node<CoordTraits<IntegerCoordinate> >::GetY)(const IntegerCoordinate&)’:
gcc-function-ptr-bug.cpp:17:27: required from ‘struct
Node<CoordTraits<IntegerCoordinate> >’
gcc-function-ptr-bug.cpp:32:42: required from here
gcc-function-ptr-bug.cpp:17:27: error: invalid conversion from ‘int (*)(const
IntegerCoordinate&)’ to ‘auto (*)(const IntegerCoordinate&)’ [-fpermissive]
17 | static constexpr auto GetY = Traits::GetY; // compilation error
| ^~~~
| |
| int (*)(const IntegerCoordinate&)
gcc-function-ptr-bug.cpp: In instantiation of ‘auto Node<Traits>::y(const
auto:2&) const [with auto:2 = IntegerCoordinate; Traits =
CoordTraits<IntegerCoordinate>]’:
gcc-function-ptr-bug.cpp:35:11: required from here
gcc-function-ptr-bug.cpp:28:46: error: use of
‘Node<CoordTraits<IntegerCoordinate> >::GetY’ before deduction of ‘auto’
28 | auto y(auto const &p) const { return GetY(p); }
| ~~~~^~~