为什么要调用dtor(使用烦人的/lambda函数)

Why is the dtor being called (using annoymous/lambda func)

本文关键字:lambda 函数 调用 dtor 为什么      更新时间:2023-10-16

我正试图模仿一种最终效果。所以我想我应该做一个快速的脏测试。

这个想法是使用Most Important const来停止破坏,并将finally块放入lambda中。然而,很明显我做错了什么,它在MyFinally()的末尾被调用了。我该如何解决这个问题?

#include <cassert>
template<typename T>
class D{
    T fn;
public:
    D(T v):fn(v){}
    ~D(){fn();}
};
template<typename T>
const D<T>& MyFinally(T t) { return D<T>(t); }
int d;
class A{
    int a;
public:
    void start(){
        int a=1;
        auto v = MyFinally([&]{a=2;});
        try{
            assert(a==1);
            //do stuff
        }
        catch(int){
            //do stuff
        }
    }
};
int main() {
    A a;
    a.start();
}

我的解决方案代码(注意:你不可能在同一个块中最终有两个。正如预期的那样。但仍然有点脏)

#include <cassert>
template<typename T>
class D{
    T fn; bool exec;
public:
    D(T v):fn(v),exec(true){}
    //D(D const&)=delete //VS doesnt support this yet and i didnt feel like writing virtual=0
    D(D &&d):fn(move(d.fn)), exec(d.exec) {
      d.exec = false;
    }
    ~D(){if(exec) fn();}
};
template<typename T>
D<T> MyFinally(T t) { return D<T>(t); }

#define FINALLY(v) auto OnlyOneFinallyPlz = MyFinally(v)
int d;
class A{
public:
    int a;
    void start(){
        a=1;
        //auto v = MyFinally([&]{a=2;});
        FINALLY([&]{a=2;});
        try{
            assert(a==1);
            //do stuff
        }
        catch(int){
            FINALLY([&]{a=3;}); //ok, inside another scope
            try{
                assert(a==1);
                //do other stuff
            }
            catch(int){
                //do other stuff
            }
        }
    }
};
void main() {
    A a;
    a.start();
    assert(a.a==2);
}

有趣的是,如果你去掉&在MyFinally中,它在原始代码中起作用。

// WRONG! returning a reference to a temporary that will be
// destroyed at the end of the function!
template<typename T>
const D<T>& MyFinally(T t) { return D<T>(t); }

你可以通过引入移动构造函数来修复它

template<typename T>
class D{
    T fn;
    bool exec;
public:
    D(T v):fn(move(v)),exec(true){}
    D(D &&d):fn(move(d.fn)), exec(d.exec) {
      d.exec = false;
    }
    ~D(){if(exec) fn();}
};

然后你可以重写你的玩具

template<typename T>
D<T> MyFinally(T t) { return D<T>(move(t)); }

希望能有所帮助。使用auto时,不需要"常量引用"技巧。请参阅此处,了解如何在C++03中使用常量引用来完成此操作。

您的代码和Sutter的代码不等价。他的函数返回一个值,而你的函数返回对一个对象的引用,该对象将在函数退出时被销毁。调用代码中的const引用不维护该对象的生存期。

如Johannes所示,问题源于函数生成器的使用。

我认为您可以通过使用另一个C++0x工具,即std::function来避免这个问题。

class Defer
{
public:
  typedef std::function<void()> Executor;
  Defer(): _executor(DoNothing) {}
  Defer(Executor e): _executor(e) {}
  ~Defer() { _executor(); }
  Defer(Defer&& rhs): _executor(rhs._executor) {
    rhs._executor = DoNothing;
  }
  Defer& operator=(Defer rhs) {
    std::swap(_executor, rhs._executor);
    return *this;
  }
  Defer(Defer const&) = delete;
private:
  static void DoNothing() {}
  Executor _executor;
};

然后,你可以简单地使用它:

void A::start() {
  a = 1;
  Defer const defer([&]() { a = 2; });
  try { assert(a == 1); /**/ } catch(...) { /**/ }
}

这个问题已经由其他人解释了,所以我会建议一个解决方案,就像Herb Sutter编写代码的方式一样(顺便说一句,你的代码和他的代码不一样):

首先,不要通过常量引用返回:

template<typename T>
D<T> MyFinally(T t) 
{
   D<T> local(t); //create a local variable
   return local; 
}

然后在呼叫站点写下:

const auto & v = MyFinally([&]{a=2;}); //store by const reference

这和赫伯·萨特的密码一模一样。

演示:http://www.ideone.com/uSkhP

现在,就在退出start()函数之前调用了析构函数。


一个不再使用auto关键字的不同实现:

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; }
};

并将其用作:

lambda finally = [&]{a=2;  std::cout << "finally executed" << std::endl; }; 

看起来很有趣?

完整演示:http://www.ideone.com/DYqrh

您可以返回shared_ptr:

template<typename T>
std::shared_ptr<D<T>> MyFinally(T t) {
    return std::shared_ptr<D<T>>(new D<T>(t));
}