[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.

yhueotnk at pokemail dot net gcc-bugzilla@gcc.gnu.org
Tue Oct 11 17:01:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77911

--- Comment #2 from Dr Hilbert Swan <yhueotnk at pokemail dot net> ---
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<Type f, int idx, bool equal>
struct FindMatchingIdx2 {
    enum { value = FindMatchingIdx2<f, idx + 1, (f == i[idx + 1])>::value };
};

template<Type f, int idx>
struct FindMatchingIdx2<f, idx, true> {
    enum { value = (idx) };
};

template<Type f>
struct FindMatchingIdx {
  enum { flag = FindMatchingIdx2<f,0, (f == i[0])>::value };
};

int main() {
    return FindMatchingIdx<test1>::flag;
}


More information about the Gcc-bugs mailing list