If you don't mind working Foo b = nullptr; It's pretty easy to hack. Have an explicit constructor from int and implicit from std::nullptr_t .
If you do not mind that you are working, I am not sure that this is possible. The only way to distinguish between literal 0 and other integer literals is to first implicitly convert to pointers and nullptr_t . Therefore, nullptr prefers the nullptr_t parameter to the pointer parameter, therefore, with both constructors, you can filter out the nullptr arguments. However, converting 0 to pointers and nullptr_t have the same rank, so this will lead to the loss of arguments 0 with ambiguity.
Hmm ... something like this might work:
class Foo { struct dummy; public: explicit Foo(int);
I have not really tried this. Theoretically, a template should only participate in overload resolution when the argument is nullptr , because otherwise SFINAE kills it. In this case, however, it should be better than a pointer constructor.
Sebastian redl
source share