Prevent the creation of temporary objects

╄→尐↘猪︶ㄣ 提交于 2019-12-08 19:46:28
Cassio Neri

Here are C++11 versions of two great answers to the same question. I don't deserve much credit for this, so consider voting for the original answers instead.

user1773602's answer

The idea is defining a deleted function with de same name as the class:

class ScopedLock {
    // ...  
};

void ScopedLock(...) = delete;

The usage is unusual and this might be (very?) inconvenient:

  class ScopedLock a; // OK             :-| but requires keyword 'class'
  ScopedLock b;       // Compiler error :-(
  ScopedLock();       // Compiler error :-)

Johannes Schaub - litb's answer

If you don't mind having a runtime error (like the OP seems to do) rather than a compile time error then you can try this.

The basic idea is that a temporary bound to an argument of ScopedLock's constructor will be destroyed before or after the ScopedLock object depending on whether the later is a temporary or not.

class ScopedLock {

  bool flag_ = true;

  struct Flag {
    bool* flag;
    ~Flag() {
      *flag = false;
    }
  };

public:

  ScopedLock(Flag&& f = Flag()) {
    f.flag = &flag_;
  }

  ~ScopedLock() {
    if (flag_) {
      std::cerr << "ScopedLock misuse\n.";
      std::terminate();
    }
  }

};

With this, we have

ScopedLock a; // OK
ScopedLock(); // Runtime error
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!