简体   繁体   English

将nullptr转换为bool

[英]Casting nullptr to bool

In the pre-C++11 days it was considered better practice to write: 在C ++之前的11天里,编写更好的做法是:

if (ptr == NULL)

Rather than: 而不是:

if (!ptr)

This was for two reasons. 这有两个原因。 It is more efficient because it didn't need to cast to bool. 它效率更高,因为它不需要强制转换为bool。 And there was no guarantee that the macro NULL would indeed evaluate to the boolean false . 并且无法保证宏NULL确实会评估为布尔值false Is this still true in C++11? 这在C ++ 11中仍然如此吗? Is it preferable to write 写作是否更可取

if (ptr == nullptr)

Rather than 而不是

if (!ptr)

Or is the second fine now? 或者现在是第二次罚款?

It is more efficient because it didn't need to cast to bool. 它效率更高,因为它不需要强制转换为bool。

I'd slap an ol' [citation-needed] at that. 我打了一个ol' [citation-needed] I doubt any modern compiler would make that count. 我怀疑任何现代编译器都能算得上。


if (ptr) is perfectly fine and probably more semantically correct, if you're using that value to represent a canonical "lack of value". if (ptr)是完全正确的,并且可能在语义上更正确,如果你使用该值来表示规范的“缺乏价值”。

I'd only expect to see if (ptr != nullptr) if someone was actually utilizing the "null" value for pointer arithmetic, but even then it's really sketchy. 我只期望看看if (ptr != nullptr)是否有人实际上使用“null”值进行指针算术,但即使这样,它也是非常粗略的。


That being said, a lot of times you can just get away with... not checking that at all. 话虽这么说,很多时候你可以逃脱...根本没有检查。 If the pointer's nullability is used to represent a nullable data field, use optional . 如果指针的可空性用于表示可为空的数据字段,请使用optional If you are initializing it in case it's empty, use value_or idiom. 如果您正在初始化它以防它为空,请使用value_or idiom。 If it's just a weak reference and the contract on value is outside, an assert will do. 如果它只是一个弱参考而且价值合约在外面,那么assert就可以了。

Smart pointers like unique_ptr and shared_ptr have implicit conversions to bool that checks the internal pointer against nullptr , so the second is preferred in those cases, because the intent is well understood. unique_ptrshared_ptr这样的智能指针具有对bool隐式转换,它将内部指针检查为nullptr ,因此在这些情况下第二个是首选,因为意图很好理解。

For raw pointers, I don't know if there's any actual guidance, but ptr != nullptr certainly explains the intent much more clearly (and what types are involved). 对于原始指针,我不知道是否有任何实际指导,但是ptr != nullptr肯定更清楚地解释了意图(以及涉及的类型)。

Re 回覆

In the pre-C++11 days it was considered better practice to write: 在C ++之前的11天里,写作更好的做法被认为是:

 if (ptr == NULL) 

Rather than: 而不是:

 if (!ptr) 

No, not by informed people. 不,不是知情人士。

This was for two reasons. 这有两个原因。 It is more efficient because it didn't need to cast to bool. 它效率更高,因为它不需要强制转换为bool。

No, the two expressions are equivalent, one is not more efficient than the other. 不,这两个表达式是等价的,一个不是比另一个更有效。 And a “cast” is a notation . 而“演员”是一种表示法 It's meaningless to confuse that with execution efficiency. 将它与执行效率混淆是毫无意义的。

And there was no guarantee that the macro NULL would indeed evaluate to the boolean false. 并且无法保证宏NULL确实会评估为布尔值false。

There is indeed such a guarantee. 确实有这样的保证。

Is this still true in C++11? 在C ++ 11中这仍然是正确的吗?

It's still all false in C++11, and in C++14, and will still be false in C++17. 它在C ++ 11和C ++ 14中仍然是错误的,在C ++ 17中仍然是错误的。

It's a matter of readability since the compiler is advanced to handle optimally both cases. 这是一个可读性的问题,因为编译器是先进的,以便在两种情况下都能得到最佳处理。 One exception is if you create your own pointer class where !ptr must be evaluated by your code and suddenly the two syntaxes are not necessary equivalent. 一个例外是如果您创建自己的指针类,其中!ptr必须由您的代码进行评估,并且突然两个语法不一定是等价的。 So I suggest when writing generic code to use if (ptr != nullptr) syntax. 所以我建议在编写通用代码时使用if (ptr != nullptr)语法。

In C as many people here said it is indeed only a matter of style whether you implicitly cast to a bool or explicitly compare against NULL . 在C中,因为这里的许多人都说,无论是隐式地转换为bool还是显式地与NULL进行比较,确实只是风格问题。

In C++11 however the story is different. 然而,在C ++ 11中,故事却不同。 nullptr provides type safety. nullptr提供类型安全性。 Consider the following examples: 请考虑以下示例:

auto ptr = accessVal();
if (ptr == nullptr) {cout << "Pointer is null."};

vs.

auto ptr = accessVal();
if (!ptr) {cout << "Pointer is null."};

The first will only compile if ptr is a pointer. 第一个只有在ptr是指针时才会编译。 The second will compile if ptr can be implicitly casted to a bool . 如果ptr可以隐式地转换为bool则第二个将编译。

To sum up the advantage of implicitly casting to bool is brevity. 总结隐含铸造bool的优点是简洁。 The advantage of comparing to nullptr is type safety. nullptr比较的优点是类型安全。 I think in most cases the benefit if type safety outweighs the benefit of brevity. 我认为在大多数情况下,如果类型安全超过简洁的好处,那么这样做会带来好处。 Indeed this is the main reason for introducing the nullptr in C++11 in the first place. 实际上,这是首先在C ++ 11中引入nullptr主要原因。

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

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