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

在有异常的程序里面,函数的出口变得难以捉摸起来,这是大家都知道的了。但是很多时候,我们希望函数在退出之前能够一定完成某些事情,在 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 采取统一命名
...
应该还有一些,不说了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值