-
总览
-
C++的异常-
C++98异常 -
C++98一致性 -
C++11 -
C++11和C++98的差异 -
接口设计
-
函数是否异常的影响
-
重要性
-
C++98和C++11差异案例[root@localhost test]# g++ test.cpp -std=c++98 [root@localhost test]# cat test.cpp #include <iostream> class T { public: T() {std::cout << __FUNCTION__ << std::endl;} ~T() {std::cout << __FUNCTION__ << std::endl;} }; void show() throw() { T t; throw 1; } int main() { show(); } [root@localhost test]# ./a.out T ~T terminate called after throwing an instance of 'int' Aborted[root@localhost test]# g++ test.cpp -std=c++11 [root@localhost test]# cat test.cpp #include <iostream> class T { public: T() {std::cout << __FUNCTION__ << std::endl;} ~T() {std::cout << __FUNCTION__ << std::endl;} }; void show() noexcept { T t; throw 1; } int main() { show(); } [root@localhost test]# ./a.out T terminate called after throwing an instance of 'int' Aborted -
编译器优化
-
小结
-
-
noexcept场景分析-
std::vector添加元素. -
正确性问题
-
C++11做法 -
案例
#include <iostream> class T { public: T() {std::cout << this << std::endl;} T(const T& t) noexcept {std::cout << "copy" << this << std::endl;} T(T&& t) noexcept {std::cout << "move" << this << std::endl;} ~T() {std::cout << this << std::endl;} }; void show(T t) noexcept(noexcept(T(std::move(t)))){ if(noexcept(T(std::move(t)))) { std::cout << "noexcept t " << &t << std::endl; T d(std::move(t)); std::cout << "noexcept d " << &d << std::endl; } else { std::cout << "except t " << &t << std::endl; T d(t); std::cout << "except d " << &d << std::endl; } } int main() { T t; show(t); }0x61fe1e copy 0x61fe1f noexcept t 0x61fe1f move 0x61fddf noexcept d 0x61fddf 0x61fddf 0x61fe1f 0x61fe1e [Finished in 485ms]#include <iostream> class T { public: T() {std::cout << this << std::endl;} T(const T& t) noexcept {std::cout << "copy" << this << std::endl;} T(T&& t) {std::cout << this << "move" << std::endl;} ~T() {std::cout << this << std::endl;} }; void show(T t) noexcept(noexcept(T(std::move(t)))){ if(noexcept(T(std::move(t)))) { std::cout << "noexcept t " << &t << std::endl; T d(std::move(t)); std::cout << "noexcept d " << &d << std::endl; } else { std::cout << "except t " << &t << std::endl; T d(t); std::cout << "except d " << &d << std::endl; } } int main() { T t; show(t); }0x61fd3e copy 0x61fd3f except t 0x61fd3f copy 0x61fbae except d 0x61fbae 0x61fbae 0x61fd3f 0x61fd3e [Finished in 472ms] -
建议
-
扩展
#include <iostream> void show(int a) noexcept(noexcept(a) && noexcept(a)){ } int main() { int a; show(a); }
-
-
优化和
noexcept -
noexcept和开发 -
函数设计
-
noexcept不一定非要noexcept实现-
wide -
wide和narrow区分 -
noexcept调用非noexcept#include<iostream> void setup(); // functions defined elsewhere void cleanup(); void doWork() noexcept { setup(); // set up work to be done // … // do the actual work cleanup(); // perform cleanup actions } int main() { doWork(); } void setup() { printf("%s\n",__FUNCTION__); } void cleanup() { printf("%s\n",__FUNCTION__); }
-
-
总结
C++11 鼓励对肯定不抛出异常的函数添加 noexcept 修饰
最新推荐文章于 2025-06-05 16:52:47 发布
本文深入探讨C++中noexcept关键字的使用与优化效果,解析其对函数行为的影响及与异常处理的关系,帮助开发者理解何时及如何应用noexcept以提升程序性能。
2万+

被折叠的 条评论
为什么被折叠?



