lundi 16 septembre 2019

'Cannot be overloaded' error while trying to enable SFINAE with enable_if

I am just trying my hands on SFINAE using 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 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.A 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: jdoodle.cpp:30:3: error: 'typename std::enable_if<(! std::is_arithmetic<_Tp>::value), T>::type Calculator::addition(T, T)' cannot be overloaded with 'typename std::enable_if::value, T>::type Calculator::addition(T, T)' addition(T a, T b) ^~~~~~~~ jdoodle.cpp:25:3: note: previous declaration 'typename std::enable_if::value, T>::type Calculator::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

Aucun commentaire:

Enregistrer un commentaire