Suppose I have a constexpr array of function pointers, and I want to write a constexpr function to find the array index for a specified function.
I might have code like this:
void test1(){}void test2(){}void test3(){}void test4(){}
typedef void(*func)(void);
constexpr func funcs[] = { &test1, &test2, &test3 };
constexpr int FindMatchingIdx (const func work, const int idx) {
return (work == funcs[idx]) ? (idx) : (FindMatchingIdx(work, idx + 1));
}
constexpr unsigned int loc = FindMatchingIdx (&test1,0);
Now this code compiles on Clang and MSVC, however GCC will only compile when FindMatchingIdx
is called with the first element in the array. If FindMatchingIdx
is called with test1
, GCC will compile the code, however if FindMatchingIdx
is called with test2
or test3
GCC will fail to compile the code, giving the error message:
error: '(test1 != test2)' is not a constant expression.
If FindMatchingIdx
has to recurse, GCC will fail to treat it as a constexpr function. Is this a bug in GCC? How does function pointer comparison even work inside a constexpr function? Obviously it can't be using real pointer values as those are assigned by the linker.
Working example: https://godbolt.org/g/xfv1PM