This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
- From: "yhueotnk at pokemail dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 11 Oct 2016 16:54:39 +0000
- Subject: [Bug c++/77911] Comparing function pointers in a constexpr function can produce an error.
- Auto-submitted: auto-generated
- References: <bug-77911-4@http.gcc.gnu.org/bugzilla/>
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;
}