大家好,我对异常不是很了解,问下问题

templateman 2010-07-28 10:54:38
namespace std {

// auxiliary type to enable copies and assignments (now global)

template<class Y>

struct auto_ptr_ref {

Y* yp;

auto_ptr_ref (Y* rhs):yp(rhs) {

}

};



template<class T>

class auto_ptr {

private:

T* ap; // refers to the actual owned object (if any)

public:

typedef T element_type;

// 构造函数

explicit auto_ptr (T* ptr = 0) throw()

: ap(ptr) {

}

// 析构函数

~auto_ptr() throw() {

delete ap;

}

// 拷贝构造函数

auto_ptr (auto_ptr& rhs) throw()

: ap(rhs.release()) {

}

template<class Y>

auto_ptr (auto_ptr<Y>& rhs) throw()

: ap(rhs.release()) {

}

// 赋值操作符

auto_ptr& operator= (auto_ptr& rhs) throw() {

reset(rhs.release());

return *this;

}

template<class Y>

auto_ptr& operator= (auto_ptr<Y>& rhs) throw() {

reset(rhs.release());

return *this;

}



// value access

T* get() const throw() {

return ap;

}

T& operator*() const throw() {

return *ap;

}

T* operator->() const throw() {

return ap;

}



// release ownership

T* release() throw() {

T* tmp(ap);

ap = 0;

return tmp;

}



// reset value

void reset (T* ptr=0) throw() {

if (ap != ptr) {

delete ap;

ap = ptr;

}

}



/* special conversions with auxiliary type to enable copies and assignments*/

auto_ptr(auto_ptr_ref<T> rhs) throw()

: ap(rhs.yp) {

}

auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new

reset(rhs.yp);

return *this;

}

template<class Y>

operator auto_ptr_ref<Y>() throw() {

return auto_ptr_ref<Y>(release());

}

template<class Y>

operator auto_ptr<Y>() throw() {

return auto_ptr<Y>(release());

}

};

}



explicit auto_ptr (T* ptr = 0) throw()

: ap(ptr) {

}
这个函数后面包含throw()是?
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
横云断岭 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yunyun1886358 的回复:]

个人认为异常的实质是一种无法预料到的,不正常的错误。比如在一个函数中,函数的输入实参、运行逻辑等等都是正确的,但是这个函数在运行时还是有可能出错,这个错误就是异常。可能这个异常时由于函数调用了其他的函数等。
[/Quote]
…………
从汇编,低层角度怎么看?
yunyun1886358 2010-07-28
  • 打赏
  • 举报
回复
个人认为异常的实质是一种无法预料到的,不正常的错误。比如在一个函数中,函数的输入实参、运行逻辑等等都是正确的,但是这个函数在运行时还是有可能出错,这个错误就是异常。可能这个异常时由于函数调用了其他的函数等。
yunyun1886358 2010-07-28
  • 打赏
  • 举报
回复
为什么要加一个throw()到你的函数中?

这是异常规范,只会出现在声明函数中,表示这个函数可能抛出任何类型的异常
void GetTag() throw(int);表示只抛出int类型异常
void GetTag() throw(int,char);表示抛出in,char类型异常
void GetTag() throw();表示不会抛出任何类型异常
void GetTag() throw(...);表示抛出任何类型异常

void GetTag() throw(int);表示只抛出int类型异常
并不表示一定会抛出异常,但是一旦抛出异常只会抛出int类型,如果抛出非
int类型异常,调用unexsetpion()函数,退出程序。

假如你加一个throw()属性到你的永远不会抛出异常的函数中,编译器会非常聪明的知道代码的意图和决定优化方式。考虑下面的代码:
class MyClass
{
size_t CalculateFoo()
{
:
:
};
size_t MethodThatCannotThrow() throw()
{
return 100;
};
void ExampleMethod()
{
size_t foo, bar;
try
{
foo = CalculateFoo();
bar = foo * 100;
MethodThatCannotThrow();
printf("bar is %d", bar);
}
catch (...)
{
}
}
};

当编译器看到这个带"throw()"属性代码的时候,编译能够优化这个"bar"变量,因为它知道从MethodThatCannotThrow()函数中不会抛出任何的异常。如果没有这个throw()属性,编译器必须创建这个"bar"变量,因为假如MethodThatCannotThrow抛出了一个异常,这个异常句柄可能会需要依靠这个bar变量。

另外,象prefast源代码分析工具能够(也会)用throw()注释去优化他们的错误检测能力----举个例子,假如你有一个try/catch而且所有调用的函数都已经标记了throw(),实际上你不需要这个try/catch(是的,假如你最后调用的函数可能抛出异常这就会有个问题了)。
templateman 2010-07-28
  • 打赏
  • 举报
回复
资源泄露吗
横云断岭 2010-07-28
  • 打赏
  • 举报
回复
借位置问下,异常的实质是什么???
thehunters 2010-07-28
  • 打赏
  • 举报
回复
高诉你这个函数要扔臭鸡蛋(异常)出来,你要捕获他

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧