jjzjj

c++ - g++ 可以检查抛出说明符吗?

关于这个的两个问题:有没有办法强制g++忽略throw说明符?(例如,我记得,VisualStudio会忽略抛出说明符,这与throw()不同)是否有可能强制g++检查抛出说明符是否正确-我的意思是检查(这可以通过one-passcompilers完成)函数是否具有throwspecifiers调用函数,可能只是通过观察他们的throwspecifiers和观察执行throw异常,这将违反说明符?(注意:这不应该监视没有抛出说明符的函数,因为这可能会导致大量警告)编辑:我将为我的第二个问题添加一些示例。假设我们有://sorryforthecodingstylehere,butIdon

c++ - 抛出并捕获 std::string

我编写了奇怪的代码,但令人惊讶的是它有效。但是现在我不知道我扔的是什么,我该如何接住它:classDate{private:intday;intmonth;intyear;intdaysPerMonth[];public:Date(intday,intmonth,intyear){intdaysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(isValidDate(day,month,year)){this->day=day;this->month=month;this->year=year;}else{throwstd::s

C++ fstream : throwing exception when reaching eof

我想读取两个文件,直到读到其中一个文件的末尾。如果出现问题,fstream应该抛出异常。问题是,设置eof位时也会设置坏位或失败位。ifstreaminput1;input1.exceptions(ios_base::failbit|ios_base::badbit);input1.open("input1",ios_base::binary|ios_base::in);ifstreaminput2;input2.exceptions(ios_base::failbit|ios_base::badbit);input2.open("input2",ios_base::binary|io

c++ - 条件运算符的值类别

考虑以下代码:intx;int&f(){returnx?x:throw0;}使用gccversion7.3.0(Ubuntu7.3.0-27ubuntu1~18.04)我得到以下编译错误:cannotbindnon-constlvaluereferenceoftype‘int&’toanrvalueoftype‘int’请注意,这在clang中编译得很好。这是(我认为是)标准中的相关声明:N4659[8.16.2.1](ConditionalOperator):Thesecondorthethirdoperand(butnotboth)isa(possiblyparenthesized

c++ - 用模板定义异常是个好主意吗?

我在想用模板定义异常是个好主意。定义不同类型的异常是一项super冗长的任务。你必须继承异常,没有任何改变,只是继承。像这样..classFooException:publicBaseException{public:...};classBarException:publicBaseException{public:...};...那是一场噩梦,不是吗?所以我正在考虑用模板定义不同的异常/**@briefExceptionofradio**/classException:publicruntime_error{private:///Nameoffilethatthrowconststr

C++基础异常题

有人可以描述一下处理以下情况的正确方法是什么吗:wchar_t*buffer=newwchar_t[...];if(!something){throwstd::runtime_error("Whatever");//Now,atthispointI'mleakingmemoryallocatedforthe'buffer'.}//Performactions.delete[]buffer;解决它的明显方法是这样的:if(!something){delete[]buffer;throwstd::runtime_error("Whatever");}现在-还好吗?(我怀疑是这样,但谁知道呢

c++ - 在包含 std::string 的函数上使用 throw() 后缀是否安全?

我有一个不抛出任何异常的成员函数,所以我在它的末尾附加了throw()后缀,表明它不会抛出任何异常。我的问题是,在函数中我使用了几个std::string,假设在std::string的初始化过程中出现了错误,并抛出bad_alloc或out_of_range(或者std::string可能出错的任何其他问题)。仍然添加throw()后缀是否仍然安全? 最佳答案 赫伯萨特says那个exceptionspecificationsconferalotlessbenefitthanthey'reworth它带来的问题多于带来的好处。所以

C++:如果抛出异常,超出范围的对象是否被销毁?

通常它会在作用域结束时被销毁。但是如果抛出异常,我可以看到会发生问题。 最佳答案 是的。C++标准n333715异常处理§15.2构造函数和析构函数1)Ascontrolpassesfromathrow-expressiontoahandler,destructorsareinvokedforallautomaticobjectsconstructedsincethetryblockwasentered.Theautomaticobjectsaredestroyedinthereverseorderofthecompletionof

c++ - 按值或引用抛出异常

来自这个答案https://stackoverflow.com/a/36738405/4523099:Athrow-expressionwithnooperandrethrowsthecurrentlyhandledexception.Theexceptionisreactivatedwiththeexistingtemporary;nonewtemporaryexceptionobjectiscreated.--ISO/IEC14882:2011Section15.1par.8那么为什么我会从这段代码中得到这个结果?代码:#includeclassmy_exception:publi

c++ - 错误 - C++ 中的 "throws different exceptions"

我收到一个错误提示error:declarationof'virtualFXHost::~FXHost()'throwsdifferentexceptionserror:thanpreviousdeclaration'virtualFXHost::~FXHost()throw()'我不确定如何开始解决这个问题,我以前从未遇到过。在我的.h中我有:public:virtual~FXHost()throw();在我的.cpp中我有:FXHost::~FXHost(){gHost=NULL;}感谢指点。 最佳答案 函数声明末尾的throw