This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[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 <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, "");

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]