C++ local class 的用途:finalizer 手法

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

在有异常的程序里面,函数的出口变得难以捉摸起来,这是大家都知道的了。但是很多时候,我们希望函数在退出之前能够一定完成某些事情,在 Java 里面,我们用 try...finally 来干这件事;而在 C++ 里面,我们有 RAII 这个好东西。不过有的时候,RAII 显得有些笨拙,如果我们希望一个函数无论如何在退出之前都要输出一个警告到控制台,难道还要为了它专门写一个类么?这不但让代码分散难于理解,而且“污染“了命名空间。

还好,我们有 local class ,这个东西看起来没什么用,但是用在这里却很合适。对于下面的这段代码,要是不用 RAII ,完成“每次退出都要执行”的动作几乎是不可能的:

#include <iostream>

#include <exception>

int func(int i)

{

try

{

switch (i)

{

case 1:

throw "i = 1";

case 2:

throw std::exception("i = 2");

case 3:

throw 3;

default:

throw std::runtime_error("default");

}

int ret = 1 / i;

return ret;

}

catch(std::runtime_error& e)

{

std::cout << "runtime_error: " << e.what() << std::endl;

}

return 0;

}

int main()

{

for ( int i = 0; i < 5; ++i )

try

{

std::cout << "func(" << i << "): " << func(i) << std::endl;

}

catch(...)

{

std::cout << "func(" << i << ") throws exception." << std::endl;

}

}

输出:

runtime_error: default

func(0): 0

func(1) throws exception.

func(2) throws exception.

func(3) throws exception.

runtime_error: default

func(4): 0

而用常规的 RAII 又不太必要,这就是 local class 显身手的地方:

#include <iostream>

#include <exception>

int func(int i)

{

struct finally

{

~finally(){ std::cout << "func() is exiting" << std::endl; }

}finalizer;

try

{

switch (i)

{

case 1:

throw "i = 1";

case 2:

throw std::exception("i = 2");

case 3:

throw 3;

default:

throw std::runtime_error("default");

}

int ret = 1 / i;

return ret;

}

catch(std::runtime_error& e)

{

std::cout << "runtime_error: " << e.what() << std::endl;

}

return 0;

}

int main()

{

for ( int i = 0; i < 5; ++i )

try

{

std::cout << "func(" << i << "): " << func(i) << std::endl;

}

catch(...)

{

std::cout << "func(" << i << ") throws exception." << std::endl;

}

}

输出:

runtime_error: default

func() is exiting

func(0): 0

func() is exiting

func(1) throws exception.

func() is exiting

func(2) throws exception.

func() is exiting

func(3) throws exception.

runtime_error: default

func() is exiting

func(4): 0

这其实也是变相的 RAII ,只不过比起“常规”实现,有一些好处:

1. 代码集中,便于理解

2. 不会污染命名空间

3. 可以对所有的 finalizer 采取统一命名

...

应该还有一些,不说了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航