Is is possible to create a class Wrapper such that
void f(void *) {}
Wrapper w;
f(w);
compiles, but
Wrapper w;
if (w) {}
doesn’t compile? Or detect the difference at runtime?
Background: A win32 HANDLE is a typedef for void *. Win32 doesn’t use NULL but instead ((HANDLE)(-1)) as an error value, so any code implicitly casting a HANDLE to bool is almost certainly testing the wrong thing. I have a class wrapping a HANDLE, and if possible I would like to remove this opportunity for errors when using the wrapper class.
In C++11 it is possible by using deleted functions. For instance:
If you are working with a compiler that does not support C++11, you can achieve the same result by declaring the
boolconversion operatorprivate:This works because accessibility of a function is verified only at the very last step of overload resolution.