Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

I want to know why this flag is needed.

From msvc’s exception handling model:

Arguments

c

When used with /EHs, the compiler assumes that functions declared as extern "C" never throw a C++ exception. It has no effect when used with /EHa (that is, /EHca is equivalent to /EHa). /EHc is ignored if /EHs or /EHa aren’t specified.

(emphasis mine)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading