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