[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 Patrick Palka changed: What|Removed |Added Status|NEW |RESOLVED Resolution|--- |FIXED CC||ppalka at gcc dot gnu.org Target Milestone|--- |12.0 --- Comment #6 from Patrick Palka --- This one is also fixed for GCC 12 by r12-6188.
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 Bug 77911 depends on bug 69681, which changed state. Bug 69681 Summary: C/C++ FEs do not consider comparisons of distinct function pointers to be constant expressions https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69681 What|Removed |Added Status|NEW |RESOLVED Resolution|--- |FIXED
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 Andrew Pinski changed: What|Removed |Added CC||tadeus.prastowo at unitn dot it --- Comment #5 from Andrew Pinski --- *** Bug 86607 has been marked as a duplicate of this bug. ***
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 Andrew Pinski changed: What|Removed |Added Last reconfirmed||2021-08-05 Depends on||69681 Ever confirmed|0 |1 Status|UNCONFIRMED |NEW --- Comment #4 from Andrew Pinski --- Confirmed, I suspect this is the same problem as PR 69681. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69681 [Bug 69681] C/C++ FEs do not consider comparisons of distinct function pointers to be constant expressions
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 --- Comment #3 from Dr Hilbert Swan --- A generous fellow offered me this workaround for the time being: void test1(){} void test2(){} void test3(){} void test4(){} #define FUNCTION_LIST test1, test2, test3, test4 template struct SearchFun { static constexpr int Idx = SearchFun::Idx; }; template struct SearchFun { static constexpr int Idx = N; }; template using FindMatchingIdx = SearchFun<0, Fun, FUNCTION_LIST>; // Tests constexpr unsigned int loc1 = FindMatchingIdx::Idx; static_assert(loc1 == 0, ""); constexpr unsigned int loc2 = FindMatchingIdx::Idx; static_assert(loc2 == 1, ""); constexpr unsigned int loc3 = FindMatchingIdx::Idx; static_assert(loc3 == 2, ""); // If the array is needed typedef void (*func)(); func funcs[] = { FUNCTION_LIST }; // If desired the template alias can be wrapped in a macro #define FindMatching(Fun) FindMatchingIdx::Idx constexpr unsigned int loc4 = FindMatching(test2); static_assert(loc4 == 1, "");
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 --- Comment #2 from Dr Hilbert Swan --- If a similar thing is done with ints the code would look like this: constexpr int i[] = { 1, 2, 3 }; constexpr int FindMatchingIdx(const int w, const int idx) { return (w == i[idx]) ? (idx) : (FindMatchingIdx(w, idx + 1)); } constexpr unsigned int loc = FindMatchingIdx(2, 0); int main() { return loc; } Which compiles correctly. (https://godbolt.org/g/mYpxn9) I've also tried float and doubles, which are fine, but there something about function pointers that gcc doesn't like. An alternative method using recursive template meta programming fails to compile with a very similar error: (https://godbolt.org/g/zanwsd) void test1(){}void test2(){} void test3(){}void test4(){} typedef void(*Type)(); constexpr Type i[] = { &test1, &test2, &test3 }; template struct FindMatchingIdx2 { enum { value = FindMatchingIdx2::value }; }; template struct FindMatchingIdx2 { enum { value = (idx) }; }; template struct FindMatchingIdx { enum { flag = FindMatchingIdx2::value }; }; int main() { return FindMatchingIdx::flag; }
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911 --- Comment #1 from Dr Hilbert Swan --- Created attachment 39777 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39777&action=edit Version that compiles.