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?