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: Wed, 12 Oct 2016 17:40:10 +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 #3 from Dr Hilbert Swan <yhueotnk at pokemail dot net> ---
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 <int N, void (&Fun)(), void (&Head)(), void (& ...Tail)()>
struct SearchFun
{
static constexpr int Idx = SearchFun<N + 1, Fun, Tail...>::Idx;
};
template <int N, void (&Fun)(), void (& ...Tail)()>
struct SearchFun<N, Fun, Fun, Tail...>
{
static constexpr int Idx = N;
};
template <void (&Fun)()>
using FindMatchingIdx = SearchFun<0, Fun, FUNCTION_LIST>;
// Tests
constexpr unsigned int loc1 = FindMatchingIdx<test1>::Idx;
static_assert(loc1 == 0, "");
constexpr unsigned int loc2 = FindMatchingIdx<test2>::Idx;
static_assert(loc2 == 1, "");
constexpr unsigned int loc3 = FindMatchingIdx<test3>::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<Fun>::Idx
constexpr unsigned int loc4 = FindMatching(test2);
static_assert(loc4 == 1, "");