简体   繁体   English

为什么无法将nullptr转换为int?

[英]Why can't nullptr convert to int?

Summary: nullptr converts to bool , and bool converts to int , so why doesn't nullptr convert to int ? 简介: nullptr转换为boolbool转换为int ,那么为什么不将nullptr转换为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?

Because it is exactly the main idea of nullptr . 因为它正是nullptr的主要思想。

nullptr was meant to avoid this behavior: nullptr旨在避免这种行为:

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. 如果nullptr可转换为int则会发生此行为。

So the question is " why is it convertible to bool ?". 所以问题是“ 为什么它可以转换为 bool ?”。

Syntax-"suggarness": Syntax- “suggarness”:

int* a = nullptr;
if (a) {
}

Which looks way better than: 哪个看起来好于:

if (a == nullptr) {
}

In §4.1 of the Standard, it says how conversions are performed: 在标准的§4.1中,它说明了如何执行转换:

Standard conversions are implicit conversions with built-in meaning. 标准转化是具有内置含义的隐式转化。 Clause 4 enumerates the full set of such conversions. 第4条列举了全套此类转换。 A standard conversion sequence is a sequence of standard conversions in the following order: 标准转换序列是一系列标准转换,顺序如下:

— Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion. - 来自以下集合的零或一次转换:左值到右值的转换,数组到指针的转换以及函数到指针的转换。

— Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions. - 来自以下集合的零或一次转换:整数促销,浮点促销,积分转换,浮点转换,浮点积分转换,指针转换,成员转换指针和布尔转换。

— Zero or one qualification conversion. - 零或一个资格转换。

So the compiler only does "zero or one conversion" of some, none, or all of each of the above types of conversions, not arbitrarily many. 因此,编译器仅对上述每种类型的转换中的某些,无或全部进行“零或一次转换”,而不是任意多次。 And that's a really good thing . 这是一件非常好的事情

To understand why is this happening, you should understand how to use nullptr . 要了解为什么会发生这种情况,您应该了解如何使用nullptr Check these links bellow: 请查看以下链接:

I hope it helps. 我希望它有所帮助。

The keyworkd nullptr is introduced in c++11 because multiple definition of the C NULL , and it confuses when overloading a function with int arguments and NULL. keyworkd nullptr是在c ++ 11中引入的,因为C NULL的多个定义,并且在使用int参数和NULL重载函数时会混淆。

#define NULL 0
#define NULL (void*)0

In the bible The C++ Programming Language (4th), page 270 在圣经“C ++编程语言”(第4版),第270页中

The pointer-to-bool conversion is useful in conditions, but confusing elsewhere. 指向bool的指针转换在条件中很有用,但在其他地方却很混乱。

So I think nullptr_t varible convert to int is not allowed because that's the reason why it exists, but it can be used as a test condition like bool variables. 所以我认为不允许将nullptr_t varible转换为int,因为这就是它存在的原因,但它可以用作bool变量之类的测试条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM