草庐IT

noexcept-ness

全部标签

c++ - gcc 与 clang : noexcept parsed in unused template specialization when static casting

我正在尝试将函数指针静态转换为特定函数重载,但似乎clang仍会解析(未使用的)模板特化的noexcept语句,从而生成编译器错误。如果未使用相应的函数重载,GCC似乎并不关心noexcept。templatevoidfun(T)noexcept(T(1)){}voidfun(int){}voidfun(int*){}intmain(){inta;fun(&a);//callingworksfinefun(a);static_cast(&fun);//staticcastingdoesn't}https://godbolt.org/z/ixpl3f这里是哪个编译器出错了?当将函数指针转

c++ - 具有按值返回和 noexcept 的函数

这个问题是"Constructorwithby-valueparameter&noexcept"的对偶问题.该问题表明,按值函数参数的生命周期管理由调用函数处理;因此调用者处理发生的任何异常,被调用函数可以将自己标记为noexcept.我想知道如何使用noexcept处理输出端.MyTypeMyFunction(SomeTypeconst&x)noexcept;//...voidMyCaller(){MyTypetest1=MyFunction(RandomSomeType());MyTypetest2{MyFunction(RandomSomeType())};//...test1=

c++ - 如何正确使用 noexcept 运算符

我已经实现了一个智能指针,它存储一个类型为T的对象,带有调用内部对象方法的代理函数:templateinlineboolcall(Function(T::*function)(Args...)const,Params&&...args)constnoexcept(noexcept(function));但是我发现了一个奇怪的问题——当一个std::exception在一个成员函数中产生时,程序终止了,即使在一个tryblock中调用了代理函数。所以我的问题是:使用noexcept运算符是否正确,如果不是,在这种情况下我应该如何使用它? 最佳答案

函数声明后的 C++ throw() (_NOEXCEPT)

我在VisualC++中看到包含文件使用throw()在函数之后:size_typecapacity()const_NOEXCEPT{//returncurrentlengthofallocatedstoragereturn(this->_Myend-this->_Myfirst);}随着_NOEXCEPTthrow()的宏,所以上面看起来像:size_typecapacity()constthrow(){//returncurrentlengthofallocatedstoragereturn(this->_Myend-this->_Myfirst);}但是throw有什么作用呢?我

C++ : noexcept (or throw()) virtual destructor = default;

下面的代码是合法的吗?classC{virtual~C()noexcept=default;};或classC{virtual~C()throw()=default;};(throw()已弃用,但我的编译器不支持noexcept;;) 最佳答案 8.4.2[dcl.fct.def.default]Anexplicitly-defaultedfunction[...]mayhaveanexplicitexception-specificationonlyifitiscompatible(15.4)withtheexception-spe

c++ - 类方法声明符中 noexcept 的行为

以下代码的预期行为是什么?使用GCC时输出为0,而使用clang时输出为1。哪一个是正确的?#includestaticconstboolne=false;structa{a()noexcept(ne){}staticconstboolne=true;};intmain(){std::cout 最佳答案 他们都是对的!这只是格式错误的代码。来自[basic.class.scope]:AnameNusedinaclassSshallrefertothesamedeclarationinitscontextandwhenre-evalu

c++ - 在接口(interface)不是时标记派生实现 noexcept 的副作用是什么

我们有一个实现IUnknown(或我们不拥有的任何接口(interface))的类。我们开始用noexcept标记我们的大部分/所有方法以进行任何潜在的优化,因为我们无论如何都不会抛出任何异常;尽管我们依赖的一些库可能。提出了QueryInterface/AddRef/Release是否应该标记为noexcept的问题,因为接口(interface)不是。当只有一些派生类被标记为noexcept时,是否有任何副作用或问题? 最佳答案 一般来说,您应该小心使用noexcept。除非编译器可以证明该函数确实不会抛出任何异常,否则它必须插

c++ - <cmath> 函数在 C++17 中必须是 `noexcept` 吗?

这个问题在这里已经有了答案:AreC++standardlibraryimplementationsallowedtostrengthennoexceptspecifications?(1个回答)关闭3年前。在优化代码的某些部分时,我决定检查是否可以将某些方法声明为noexcept,这归结为我对中的数学函数的不完整了解.所以,我决定检查是否sin()和asin()(作为示例)我正在使用的实际上是noexcept.static_assert(noexcept(asin(1)));static_assert(noexcept(sin(1)));成功通过,所以他们实际上是noexcept。我

c++ - 如何在 MSVC 下检测 C++11 的 noexcept 特性?

我正在使用C++库。该库的最低要求是C++03。我在VisualStudio2015下发现了一些关于抛出析构函数的警告:...algparam.h(271):warningC4297:'AlgorithmParametersBase::~AlgorithmParametersBase':functionassumednottothrowanexceptionbutdoes...algparam.h(271):note:destructorordeallocatorhasa(possiblyimplicit)non-throwingexceptionspecificationthrow是

c++ - lambda 表达式的 noexcept 和空抛规范之间有什么区别吗?

举个例子:doublevalues[]{2.5,-3.5,4.5,-5.5,6.5,-7.5};std::vectorsquares(std::end(values)-std::begin(values));std::transform(std::begin(values),std::end(values),std::begin(values),std::begin(squares),[](doublex1,doublex2)throw(){returnx1*x2;});这在功能上等同于以下内容吗?[](doublex1,doublex2)noexcept{returnx1*x2;})