相关疑难解决方法(0)

为什么nullptr不能转换为int?

总结:nullptr转换为bool,bool转换为int,为什么不nullptr转换为int

这段代码没问题:

void f(bool);
f(nullptr);      // fine, nullptr converts to bool
Run Code Online (Sandbox Code Playgroud)

这没关系:

bool b;
int i(b);        // fine, bool converts to int
Run Code Online (Sandbox Code Playgroud)

那么为什么这不行呢?

void f(int);
f(nullptr);      // why not convert nullptr to bool, then bool to int?
Run Code Online (Sandbox Code Playgroud)

c++ nullptr implicit-conversion c++11

16
推荐指数
2
解决办法
6432
查看次数

标签 统计

c++ ×1

c++11 ×1

implicit-conversion ×1

nullptr ×1