我正在撰寫一個簡單的代碼來學習 sfinae。但是,它僅在 Visual Studio 中生成警告(而不是在使用 g 時)。
代碼是:
#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;
}
警告是:
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
]
使用 flag 編譯或從兩個函式中/EHsc洗掉時,警告消失。cout
我想知道為什么需要這個標志。
此致。
uj5u.com熱心網友回復:
我想知道為什么需要這個標志。
來自msvc 的例外處理模型:
論據
c與 一起使用時
/EHs,編譯器假定宣告為extern "C"的函式從不拋出 C 例外。與 /EHa 一起使用時無效(即 /EHca 等效于 /EHa)。如果未指定 /EHs 或 /EHa,則忽略 /EHc。
(強調我的)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520999.html
標籤:C c 11
