vendredi 25 janvier 2019

GCC does not disable a function based on SFINAE rules

On compiling the following Try it out on Coliru!, I was expecting that GCC does not consider the function

  template <typename DST, typename... Ts> 
  std::enable_if_t<sizeof...(Ts) == 0> CheckAndSetVal(DST&) {}

for analysis because the sizeof condition is not fulfilled.

namespace SFINAE
{
  template <typename DST, typename... Ts> 
  std::enable_if_t<sizeof...(Ts) == 0> CheckAndSetVal(DST&) {}

  template <typename DST, typename T1, typename T2, typename... Ts>
  std::enable_if_t<!std::is_same_v<DST, T2> > CheckAndSetVal(DST& dst, T1&& cond, T2&& val, Ts&&... ts)
  {
    if (cond())
      dst = val();
    else
      CheckAndSetVal(dst, std::forward<Ts>(ts)...);
  }

  template <typename DST, typename T1, typename T2, typename... Ts>
  std::enable_if_t<std::is_same_v<DST, T2> > CheckAndSetVal(DST& dst, T1&& cond, T2&& val, Ts&&... ts)
  {
    if (cond())
      dst = val;
    else
      CheckAndSetVal(dst, std::forward<Ts>(ts)...);
  }

  template <typename DST, typename... Ts>
  void SetValue(DST& dst, Ts&&... ts)
  {
    CheckAndSetVal(dst, std::forward<Ts>(ts)...);
  }
}

int main()
{
  int i = 0;
  SFINAE::SetValue(i, []() { return true; }     , []() { return 222; }
                    , []() { return false; }    , 444
                  );
  std::cout << "Value using SFINAE::SetValue:" << i << std::endl;
}

But I see GCC throwing the following error which IMHO is a contradiction to itself. It complains that it cannot find a matching function for one of the recursive function call with int&, lambda and int as arguments. But again says that the candidate is the one which should have been disabled because the condition, sizeof...(Ts) == 0 is false.

candidate expects 1 argument, 3 provided

Can someone help me understand why this is so ?

main.cpp: In instantiation of 'std::enable_if_t<(! is_same_v<DST, T2>)> SFINAE::CheckAndSetVal(DST&, T1&&, T2&&, Ts&& ...) [with DST = int; T1 = main()::<lambda()>; T2 = main()::<lambda()>; Ts = {main()::<lambda()>, int}; std::enable_if_t<(! is_same_v<DST, T2>)> = void]':
main.cpp:36:19:   required from 'void SFINAE::SetValue(DST&, Ts&& ...) [with DST = int; Ts = {main()::<lambda()>, main()::<lambda()>, main()::<lambda()>, int}]'
main.cpp:47:19:   required from here
main.cpp:21:21: error: no matching function for call to 'CheckAndSetVal(int&, main()::<lambda()>, int)'
       CheckAndSetVal(dst, std::forward<Ts>(ts)...);
       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:40: note: candidate: 'template<class DST, class ... Ts> std::enable_if_t<(sizeof... (Ts) == 0)> SFINAE::CheckAndSetVal(DST&)'
   std::enable_if_t<sizeof...(Ts) == 0> CheckAndSetVal(DST&) {}
                                        ^~~~~~~~~~~~~~
main.cpp:13:40: note:   template argument deduction/substitution failed:
main.cpp:21:21: note:   candidate expects 1 argument, 3 provided
       CheckAndSetVal(dst, std::forward<Ts>(ts)...);
       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:16:47: note: candidate: 'template<class DST, class T1, class T2, class ... Ts> std::enable_if_t<(! is_same_v<DST, T2>)> SFINAE::CheckAndSetVal(DST&, T1&&, T2&&, Ts&& ...)'
   std::enable_if_t<!std::is_same_v<DST, T2> > CheckAndSetVal(DST& dst, T1&& cond, T2&& val, Ts&&... ts)
                                               ^~~~~~~~~~~~~~
main.cpp:16:47: note:   template argument deduction/substitution failed:

Aucun commentaire:

Enregistrer un commentaire