Search code examples
c++templatestemplate-meta-programmingsfinaegeneric-programming

How to use enable if with template arguments and parameter pack?


I was working around with c++ templates and came across this piece of code which is using SFINAE using std::enable_if. I am facing two issues with this code.

#include <string>
#include <iostream>

enum class Type : int { First = 1, Second, Third };

template <Type T>
struct Symbol : public std::true_type {
  using Parent = std::true_type;
  using type = Parent::value_type;
  static constexpr type value = Parent::value;
  static constexpr type GetValue() {
        if constexpr (T == Type::First) return value;
    else return !value;
  }
};


template <bool b>
using EnableIf = typename std::enable_if<b, int>::type;

template <Type T> static constexpr bool IsTradingSymbol() {
  return Symbol<T>::GetValue();
}

template <Type T, EnableIf<IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
  return true;
}
template <Type T, EnableIf<!IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
  return false;
}

int main () {
  std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
  std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
  return 0;
}

1) Why parameter pack used in EnableIf<IsTradingSymbol<T>()>.... If I remove the ... then the code fails in the compilation. I am unable to deduce the exact error from the trace and why this ... is needed at all there? (compiled with GCC 7.4.0).

enableif.cpp: In function ‘int main()’:
enableif.cpp:43:54: error: no matching function for call to ‘GetErrorForUi<First>(Type)’
   std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
                                                      ^
enableif.cpp:26:6: note: candidate: template<Type T, typename std::enable_if<IsTradingSymbol<T>(), int>::type <anonymous> > bool GetErrorForUi(Type)
 bool GetErrorForUi(Type A) {
      ^~~~~~~~~~~~~
enableif.cpp:26:6: note:   template argument deduction/substitution failed:
enableif.cpp:43:54: note:   couldn't deduce template parameter ‘<anonymous>’
   std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
                                                      ^
enableif.cpp:30:6: note: candidate: template<Type T, typename std::enable_if<(! IsTradingSymbol<T>()), int>::type <anonymous> > bool GetErrorForUi(Type)
 bool GetErrorForUi(Type A) {
      ^~~~~~~~~~~~~
enableif.cpp:30:6: note:   template argument deduction/substitution failed:
enableif.cpp:43:54: note:   couldn't deduce template parameter ‘<anonymous>’
   std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
                                                      ^
enableif.cpp:44:56: error: no matching function for call to ‘GetErrorForUi<Second>(Type)’
   std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
                                                        ^
enableif.cpp:26:6: note: candidate: template<Type T, typename std::enable_if<IsTradingSymbol<T>(), int>::type <anonymous> > bool GetErrorForUi(Type)
 bool GetErrorForUi(Type A) {
      ^~~~~~~~~~~~~
enableif.cpp:26:6: note:   template argument deduction/substitution failed:
enableif.cpp:44:56: note:   couldn't deduce template parameter ‘<anonymous>’
   std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
                                                        ^
enableif.cpp:30:6: note: candidate: template<Type T, typename std::enable_if<(! IsTradingSymbol<T>()), int>::type <anonymous> > bool GetErrorForUi(Type)
 bool GetErrorForUi(Type A) {
      ^~~~~~~~~~~~~
enableif.cpp:30:6: note:   template argument deduction/substitution failed:
enableif.cpp:44:56: note:   couldn't deduce template parameter ‘<anonymous>’
   std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;

2) The code is not compiling with icc X86-64 (intel compiler 19.0.1). Does the compiler not support std::enable_ifsfinae or is it a bug with the intel compiler itself?


Solution

  • 1) Why parameter pack used in EnableIf()>.... If I remove the ... then the code fails in the compilation. I am unable to deduce the exact error from the trace and why this ... is needed at all there? (compiled with GCC 7.4.0).

    Look at EnableIf<IsTradingSymbol<T>()>.

    It is

    template <bool b>
    using EnableIf = typename std::enable_if<b, int>::type;
    

    So is int when the condition is true, nothing otherwise.

    Suppose the condition IsTradingSymbol<T>() is true; so

    template <Type T, EnableIf<IsTradingSymbol<T>()>...>
    bool GetErrorForUi(Type A) {
      return true;
    }
    

    become

    template <Type T, int ...>
    bool GetErrorForUi(Type A) {
      return true;
    }
    

    Now you have a template function. It requires some, non deducible from the arguments, parameters: a Type (T), that is mandatory, and a list of integers, zero or more.

    You call the function as follows

    GetErrorForUi<Type::First>(Type::First);
    

    So you pass to the function, as template parameter, only a Type. Non integers.

    This works because the function expect zero or more integers.

    But when you remove the ellipsis (...), the function become

    template <Type T, int>
    bool GetErrorForUi(Type A) {
      return true;
    }
    

    Now GetErrorForUi() expect two template parameter: a Type and a int. Exactly one int, no more zero or more.

    Now one integer is mandatory and only one is acceptable.

    So now the call

    GetErrorForUi<Type::First>(Type::First);
    

    doesn't works (gives compilation error) because you don't pass the mandatory template int parameter.

    And also

    GetErrorForUi<Type::First, 0, 1>(Type::First);
    

    doesn't works (after the ellipsis removing; before should compile) because the functions expects exactly one integer and you pass two ints.

    This should answer also to your second point.