new (nothrow) T() 的原理

来源:互联网 发布:返利网淘宝购物车付款 编辑:程序博客网 时间:2024/05/18 02:41

查看<new>中对new的声明.

void* operator new(std::size_t) throw (std::bad_alloc);

void* operator new(std::size_t, const std::nothrow_t&) throw();

void* operator new[](std::size_t) throw (std::bad_alloc);

void* operator new[](std::size_t, const std::nothrow_t&) throw();

 

如果使用,new T () 程序,调用 void* operator new(std::size_t) throw (std::bad_alloc); 在内存不足的情况下,将抛出bad_alloc异常.

如果使用,new (nothrow) T () 程序,调用 void* operator new(std::size_t, const std::nothrow_t&) throw(); 拥有空异常修饰符,不允许抛出任何异常,其定义中,如果内存不足返回值为0.

 

 C++11标准中:

throw() 已经被弃用,而使用noexcept “异常声明” 替代。

void f() noexcept {...} // 函数不抛出任何异常,如果函数体出现异常,程序直接中断

void f() noexcept(true){...} // 同上

void f() noexcept(false) {...} // 函数允许抛出异常,如果函数提出现异常,将向上抛出异常


C++11中取消了:void f() throw (int ,double) {...}  这种指定函数抛出异常类型的方式,以void f() noexcept(false) {...}  取代。


但,目前看来,支持C++11标准的编译器实际上也没有准训以上标准,任然使用以前的格式。更多可以参见gnu 4.8.2源码



[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*  
  2.  * File:   Player.h 
  3.  * Author: Vicky.H 
  4.  * Email:  eclipser@163.com 
  5.  * 
  6.  * Created on 2014年1月21日, 下午7:17 
  7.  */  
  8.   
  9. #ifndef PLAYER_H  
  10. #define PLAYER_H  
  11.   
  12. #define NDEBUG  
  13.   
  14. #if defined(__cplusplus) && (__cplusplus >= 201103) // C++11  
  15.     #ifndef NDEBUG  
  16.         #define VNOECEPT noexcept(true)  // debug版本,不允许析构函数等抛出异常  
  17.     #else  
  18.         #define VNOECEPT noexcept(false) // release版本,允许析构函数等抛出异常  
  19.     #endif  
  20. #else  
  21.     #ifndef NDEBUG  
  22.         #define VNOECEPT throw()  // debug版本,不允许析构函数等抛出异常  
  23.     #else  
  24.         #define VNOECEPT          // release版本,允许析构函数等抛出异常  
  25.     #endif  
  26. #endif  
  27.   
  28. class Player {  
  29. public:  
  30.     Player();  
  31.     Player(const Player& orig);  
  32.     virtual ~Player() VNOECEPT /**一般析构函数都不允许抛出异常,*/;  
  33. private:  
  34.   
  35. };  
  36.   
  37. #endif  /* PLAYER_H */  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*  
  2.  * File:   Player.cpp 
  3.  * Author: Vicky.H 
  4.  * Email:  eclipser@163.com 
  5.  *  
  6.  * Created on 2014年1月21日, 下午7:17 
  7.  */  
  8.   
  9. #include "Player.h"  
  10.   
  11. Player::Player() {  
  12. }  
  13.   
  14. Player::Player(const Player& orig) {  
  15. }  
  16.   
  17. Player::~Player() VNOECEPT {  
  18.     throw "~Player() not allow throw exception, but throwed";  
  19. }  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*  
  2.  * File:   main7.cpp 
  3.  * Author: Vicky.H 
  4.  * Email:  eclipser@163.com 
  5.  */  
  6. #include <iostream>  
  7. #include <new>  
  8. #include "Player.h"  
  9.   
  10. void f1() throw () {  
  11.     std::cout << "f1()" << std::endl;  
  12. }  
  13.   
  14. // C++11标准  
  15.   
  16. void f2() noexcept { // 不抛出异常 noexcept = noexcept(true)  
  17.     std::cout << "f2()" << std::endl;  
  18. }  
  19.   
  20. template <typename T>  
  21. const T& max(const T& o1, const T& o2) noexcept(false) { // 要抛出异常  
  22.     return o1 > o2 ? o1 : o2;  
  23. }  
  24.   
  25. template<>  
  26. const int& max(const int& o1, const int& o2) noexcept(false){ // noexcept(true) ERROR 当模版设置为要抛出异常,儿模版的范例却不抛出异常将导致错误  
  27.     return o1 < o2 ? o1 : o2;  
  28. }  
  29.   
  30. /* 
  31.  *  
  32.  */  
  33. int main(void) {  
  34.     f1();  
  35.     f2();  
  36.     std::cout << "\n---------------------------" << std::endl;  
  37.     std::cout << max(1.0, 2.0) << std::endl;  
  38.     std::cout << max(1, 2) << std::endl;  
  39.     std::cout << "\n---------------------------" << std::endl;  
  40.       
  41.     try {  
  42.         Player* p = new Player();  
  43.         delete p;  
  44.     } catch (...) {  
  45.         std::cout << "except happen" << std::endl;  
  46.     }  
  47.     return 0;  
  48. }  
0 0
原创粉丝点击