【发布时间】:2019-05-23 23:18:16
【问题描述】:
我正在从 std::exception 派生一个类,但出现错误 这是我的代码
#include "stdafx.h"
#include <iostream>
using namespace std;
class exp1 : public std::exception {
public:
exp1() noexcept = default;
~exp1() = default;
virtual const char* what() const noexcept
{
return "This is an exception";
}
};
int main()
{
try{
int i;
cin >> i;
if(i == 0)throw exp1() ;
else {cout << i << endl;}
}
catch(exp1 & ex){
cout << ex.what() << endl;
}
return 0;
}
我的代码工作正常,但是当我在构造函数中包含 noexcept 时
exp1() noexcept = default;
然后我得到错误
'exp1::exp1(void) noexcept': attempting to reference a deleted function
和
the declared exception specification is incompatible with the generated one
【问题讨论】:
-
什么编译器?什么编译器版本?请发帖minimal reproducible example。
-
我用的是VS2015
-
@PaulMcKenzie 我的代码有什么问题?我正在使用 VS2015
-
适用于 gcc/clang Demo。
-
如果我不得不猜测,我怀疑在 VC2015 实现中
std::exception的默认构造函数没有标记为noexcept。exp1默认构造函数调用它,所以不能是noexcept本身。