大家好,欢迎来到IT知识分享网。
noexcept
一.noexcept简介
noexcept 是一个 C++11 引入的关键字,用于表示一个函数或表达式在执行期间是否会抛出异常。它可以作为函数的一部分声明,也可以作为表达式的一部分使用。
1.特点:
- 提高代码效率:如果一个函数使用了 noexcept声明,编译器会认为这个函数不会抛出异常,从而可以对这个函数进行一些优化,例如避免额外的堆栈操作和异常处理代码等,从而提高代码效率。
- 帮助程序员调试:如果一个函数没有使用 noexcept声明,并且在函数内部抛出了异常,这个异常会在函数的调用栈上一路传递直到被捕获,如果捕获不到,程序就会崩溃。而如果使用了 noexcept声明,那么一旦函数内部抛出了异常,程序就会直接调用 std::terminate()函数终止程序,这可以帮助程序员快速发现和调试程序中的异常问题。
- 帮助编写高质量代码:使用 noexcept声明可以帮助编写高质量的代码,因为它可以明确表示函数是否会抛出异常,从而让函数的调用者更容易正确地处理异常情况。
2.使用场景:
- 用于表示函数不会抛出异常,从而提高代码效率和可读性。
- 用于表示函数可能会抛出异常,从而提醒函数的调用者需要正确地处理异常情况。
- 用于重载函数,以区分抛出异常和不抛出异常的版本。这样可以让函数调用者在需要处理异常情况时选择正确的版本。
- 用于模板函数和类型推导,可以帮助编译器更好地推导函数和表达式的类型和特征。
- 在编写 RAII(资源获取即初始化)类时,需要保证在析构函数中不会抛出异常,否则可能导致资源无法正确释放。此时可以使用 noexcept 来确保析构函数不会抛出异常。
- 在使用 move 语义的情况下,移动构造函数和移动赋值函数应该保证不会抛出异常,因此可以使用 noexcept 来标记这些函数。
- 在使用标准库中的算法和容器时,需要注意一些函数是否保证不会抛出异常,例如 std::vector::resize 和 std::unique_ptr::reset 等函数。
二.noexcept用法示例
1.表示函数不会抛出异常:
// 使用 noexcept 声明函数不会抛出异常,编译器可以进行优化 int sum(const std::vector<int>& vec) noexcept {
int result = 0; for (const auto& i : vec) {
result += i; } return result; }
2.表示函数可能会抛出异常:
// 使用异常说明符声明函数可能会抛出异常,从而提醒函数的调用者需要正确地处理异常情况。 double divide(double a, double b) throw(std::runtime_error) {
if (b == 0.0) {
throw std::runtime_error("Divide by zero"); } return a / b; }
3.用于重载函数,以区分抛出异常和不抛出异常的版本:
// 重载函数,分别用 noexcept 和 throw 声明不同版本的函数 void print(int x) noexcept {
std::cout << "No exception version: " << x << '\n'; } void print(int x) throw(std::runtime_error) {
if (x < 0) {
throw std::runtime_error("Negative number not allowed"); } std::cout << "Exception version: " << x << '\n'; }
4.用于模板函数和类型推导:
// 使用模板和 auto 关键字,同时使用 noexcept 限定模板参数类型的特征 template <typename T> auto sum(const T& container) noexcept(std::is_nothrow_copy_constructible_v<typename T::value_type>) {
typename T::value_type result{
}; for (const auto& x : container) {
result += x; } return result; } int main() {
std::vector<int> vec{
1, 2, 3, 4}; std::cout << "Sum of vector: " << sum(vec) << '\n'; std::array<float, 3> arr{
1.0f, 2.0f, 3.0f}; std::cout << "Sum of array: " << sum(arr) << '\n'; return 0; }
5.编写 RAII 类:
// 使用 noexcept 声明析构函数不会抛出异常 class RAII {
public: explicit RAII(int* ptr) noexcept : ptr_(ptr) {
} ~RAII() noexcept {
if (ptr_ != nullptr) {
delete ptr_; } } private: int* ptr_; }; int main() {
RAII raii(new int(42)); return 0; }
6.使用移动语义:
// 使用 noexcept 声明移动构造函数和移动赋值函数不会抛出异常 class MyClass {
public: MyClass() = default; MyClass(MyClass&& other) noexcept {
// ... } MyClass& operator=(MyClass&& other) noexcept {
if (this != &other) {
// ... } return *this; } }; int main() {
MyClass a; MyClass b(std::move(a)); b = std::move(a); return 0; }
7.使用标准库容器:
#include <vector> #include <iostream> // 使用 noexcept 声明函数不会抛出异常 void resize_vec(std::vector<int>& vec, const size_t size) noexcept {
try {
vec.resize(size); } catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << '\n'; } } int main() {
std::vector<int> vec{
1, 2, 3}; resize_vec(vec, 2); for (const auto& i : vec) {
std::cout << i << ' '; } std::cout << '\n'; return 0; }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/118037.html