dimanche 23 octobre 2022

Warning in visual studio in simple sfinae code

I am writing a simple code for learning sfinae. However, it generates a warning only in Visual Studio (not when using g++).

The code is:

#include <iostream>
#include <type_traits>

template <class T, typename std::enable_if<std::is_integral<T>::value, nullptr_t>::type = nullptr>
void f(T t)
{
    std::cout << "Integer" << std::endl;
    return;
}

template <class T, typename std::enable_if<!std::is_integral<T>::value, nullptr_t>::type = nullptr>
void f(T t)
{
    std::cout << "not integer" << std::endl;
    return;
}


int main()
{
    f(10);
    f(5.5);
    f("Hello");

    return 0;
}

And the warning is:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\ostream(746): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
sfinae2.cpp(9): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' being compiled
sfinae2.cpp(23): note: see reference to function template instantiation 'void f<int,nullptr>(T)' being compiled
        with
        [
            T=int
        ]                                                       

The warning disappears when compiling with the flag /EHsc, or when deleting the cout from both functions.

I want to know why this flag is needed.

Best regards.

Aucun commentaire:

Enregistrer un commentaire