In some of the unique_lock constructors in C++11 one can pass some classes like a flag, i.e.
auto lock = std::unique_lock<std::mutex> lock(m, std::defer_lock);
where std::defer_lock is defined as
struct defer_lock {}
Why is it done this way, and not with an enum?
I tried to apply this to a small code sample, but I couldn’t get it compiling:
class A {};
void foo(A a) {}
int main() {
foo(A); // error: 'A' does not refer to a value
}
When I put the parentheses like foo(A()); it works, but I don’t see the difference to the STL. Why does this behave differently there?
Using a different type to mark a certain operation rather than an
enummakes the choice of code path being taking a compile-time choice rather than a run-time choice. The implementations of the different functions can also be drastically different.