灵感来自 the other topic ,我写了这段代码来模拟 finally
block :
#include <cassert>
#include <iostream>
struct base { virtual ~base(){} };
template<typename TLambda>
struct exec : base
{
TLambda lambda;
exec(TLambda l) : lambda(l){}
~exec() { lambda(); }
};
class lambda{
base *pbase;
public:
template<typename TLambda>
lambda(TLambda l): pbase(new exec<TLambda>(l)){}
~lambda() { delete pbase; }
};
class A{
int a;
public:
void start(){
int a=1;
lambda finally = [&]{a=2; std::cout<<"finally executed";};
try{
assert(a==1);
//do stuff
}
catch(int){
//do stuff
}
}
};
int main() {
A a;
a.start();
}
输出(ideone):
finally executed
@Johannes 似乎认为它不完全正确,commented that :
It can crash if the compiler doesn't elide the temporary in the copy initialization, because then it deletes twice with the same pointer value
我想知 Prop 体是怎么回事。帮助我理解问题:-)
编辑:
问题修复为:
class lambda{
base *pbase;
public:
template<typename TLambda>
lambda(TLambda l): pbase(new exec<TLambda>(l)){}
~lambda() { delete pbase; }
lambda(const lambda&)= delete; //disable copy ctor
lambda& operator=(const lambda&)= delete; //disable copy assignment
};
然后将其用作:
//direct initialization, no copy-initialization
lambda finally([&]{a=2; std::cout << "finally executed" << std::endl; });
最佳答案
在这个初始化中:
lambda finally = [&]{a=2; std::cout<<"finally executed";};
可以使用 lambda
的隐式定义的复制构造函数。这将只复制原始指针 pbase
,然后将多次删除它。
例如
$ g++ -std=c++0x -Wall -Wextra -pedantic -fno-elide-constructors lambdafun.cc
$ ./a.out
a.out: lambdafun.cc:29: void A::start(): Assertion `a==1' failed.
finally executedAborted (core dumped)
实际上,您的断言触发掩盖了双重删除问题,但这证明了我强调的崩溃。
$ g++ -std=c++0x -Wall -Wextra -pedantic -fno-elide-constructors -DNDEBUG lambdafun.cc
$ ./a.out
Segmentation fault (core dumped)
关于c++ - 在 C++0x 中模拟 finally block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6167515/