Why can't nullptr convert to int?
c++, c++11, implicit-conversion, nullptr
Solution
Because it is exactly the main idea of `nullptr`.
`nullptr` was meant to avoid this behavior:
struct myclass {};
void f(myclass* a) { std::cout << "myclass\n"; }
void f(int a) { std::cout << "int\n"; }
// ...
f(NULL); // calls void f(int)
If `nullptr` were convertible to `int` this behavior would occur.
So the question is "why is it convertible to `bool`?".
Syntax-"suggarness":
int* a = nullptr;
if (a) {
}
Which looks way better than:
if (a == nullptr) {
}
Problem
Summary: `nullptr` converts to `bool`, and `bool` converts to `int`, so why doesn't `nullptr` convert to `int`? This code is okay: ``` void f(bool); f(nullptr); // fine, nullptr converts to bool ``` And this is okay: ``` bool b; int i(b); // fine, bool converts to int ``` So why isn't this okay? ``` void f(int); f(nullptr); // why not convert nullptr to bool, then bool to int? ```