What does (void *)1 mean?

This is an old trick to avoid problems with implicit conversions to bool from before explicit contextual conversions were introduced in C++11. It's intended to be used to check validity:

Subscriber my_subscriber = someFunction();
if (!my_subscriber) {
    // error case
}

The important point is that no built-in conversion exists from void* to integer types, but one does exist from bool to integer types. At the same time, a built-in conversion from void* to bool exists. That means that if you define an implicit conversion to bool, then the following is surprisingly valid:

void my_func(int i);

void another_func() {
    Subscriber sub = something();
    my_func(sub);
}

Defining a conversion to void* avoids that issue.


These days that trick is obsolete though. C++11 introduced explicit conversions. explicit conversions to bool are considered in the conditions of if and loops, but aren't considered in other problematic cases. That means that these days that conversion should be written as:

explicit operator bool() const { return impl_ && impl_->isValid(); }