C++备忘录060:noexcept gist of "Effective Modern C++"

int f(int x) throw();   // C++98
int f(int x) noexcept;  // C++11

If exception specificatioin is violated,

C++98: the call stack is unwound to f's caller, and, after some actions not relevant here, program execution is terminated.

C++11: the stack is only possibly unwound before program execution is terminated.

In a noexcept function, optimizers need not keep the runtime stack, nor must they ensure that objects in a noexcept function are destroyed.

std::vector::push_back takes advantage of this “move if you can, but copy if you must” strategy, and other functions sporting the strong exeption safety guarantee in C++98 behave the same way.

All these functions replace calls to copy operations in C++98 with calls to move operations in C++11 only if the move operations are known to not emit exeptions, which by calling std::move_if_noexcept, which calls std::is_nothrow_move_constructible which based on whether the move constructor has a noexcept/throw() designation.

In C++98, it was considered bad style to permit the memory deallocation function (i.e., operator delete and operator delete[]) and destructors to emit exceptions.

In C++11, all memory deallocation functions and all destructors – both user-defined and compiler-generated – are implicitly noexcept

If the destructor for an object being used by the STL emits an exception, the behavior of the program is undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值