首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教函数 static int min() throw()中的throw() 是什么意思

2012-03-16 
请问函数 static int min() throw()中的throw() 是什么意思?staticintmin()throw(){return-2147483648}请

请问函数 static int min() throw()中的throw() 是什么意思?
static   int   min()   throw()
  {
          return   -2147483648   ;
  }
请问这个函数中   throw()是什么意思?起什么作用?

[解决办法]
说明这个函数不抛出任意异常
[解决办法]
我也是菜鸟,不过在网上找到这样的一段话,希望对你有点帮助

为什么要加一个throw()到你的函数中?

假如你加一个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(是的,假如你最后调用的函数可能抛出异常这就会有个问题了)。




热点排行