I am just trying my hands on SFINAE using std::enable_if
in C++. I thought i understood the theory part until i couldn't get the following piece of code to compile. Adding to this confusion is the different behaviour on Visual studio and Linux. This pasted code compiles on VS as long as you don't uncomment (Calculator<int> cInt;
). However, using GCC it gives me compilation error. I already see this kind of code in STL implementations, i was really expecting more standardized implementations everywhere. Anyways, can you please check and suggest what are the gaps in my understanding here?
template<typename T>
class Calculator
{
public:
typename enable_if<is_arithmetic<T>::value, T>::type
addition(T a, T b)
{
return a + b;
}
typename enable_if<!is_arithmetic<T>::value, T>::type
addition(T a, T b)
{
cout << "Default\n";
return a;
}
};
void SFINAE()
{
// Calculator<int> cInt;
}
int main ()
{
SFINAE();
return 0;
}
Error log with GCC 8.1: j
doodle.cpp:30:3: error: 'typename std::enable_if<(! std::is_arithmetic<_Tp>::value), T>::type Calculator<T>::addition(T, T)' cannot be overloaded with 'typename std::enable_if<std::is_arithmetic<_Tp>::value, T>::type Calculator<T>::addition(T, T)'
addition(T a, T b)
^~~~~~~~
jdoodle.cpp:25:3: note: previous declaration 'typename std::enable_if<std::is_arithmetic<_Tp>::value, T>::type Calculator<T>::addition(T, T)'
addition(T a, T b)
Error log on VS when you uncomment Calculator class initialization with int:
sfinae.h(17): error C3646: 'addition': unknown override specifier
sfinae.h(17): error C2059: syntax error: '('
sfinae.h(18): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
template <int&... ExplicitArgumentBarrier, typename U = T>
(which is what abseil does). – Bigoted