Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why do you use not_null in C++ to indicate that null is invalid?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains why not_null is used in C++ to show that empty is invalid value. The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why not_null is used in C++ to show that empty value is invalid.

Reason (reason)

Clarity. A function that uses the not_null parameter makes it clear that the caller is responsible for null pointer checking if necessary. Similarly, the function that returns not_null makes it clear to the caller that there is no need for nullptr checking.

Example (sample)

Not_null makes it obvious to a reader (human or machine) that a test for nullptr is not necessary before dereference. Additionally, when debugging, owner and not_null can be instrumented to check for correctness.

Not_null indicates to the reader (human or machine) that there is no need for nullptr checking before dereferencing. In addition, onwer and not_null can be used for correctness checking when debugging.

Consider: (consider the following code:)

Int length (Record* p)

When I call length (p) should I check if p is nullptr first? Should the implementation of length () check if p is nullptr?

Do you need to first check whether p is nullptr when calling length (p)? Should I check if p is nullptr when implementing length ()?

/ / it is the caller's job to make sure p! = nullptrint length (not_null p)

/ / the implementor of length () must assume that p = = nullptr is possibleint length (Record* p)

Not_null is assumed not to be nullptr;T*, maybe nullptr; both behave as T* in memory (so there is no extra cost).

Note (Note)

Not_null is not just for built-in pointers. It works for unique_ptr, shared_ptr, and other pointer-like types.

Not_null is not just for built-in pointers. It also applies to unique_ptr and shared_ptr and other similar pointer types.

Enforcement (implementation recommendations)

(Simple) Warn if a raw pointer is dereferenced without being tested against nullptr (or equivalent) within a function, suggest it is declared not_null instead.

A naked pointer in a function will alarm if it is dereferenced without a nullptr (or equivalent) check. The recommended definition is not_null.

(Simple) Error if a raw pointer is sometimes dereferenced after first being tested against nullptr (or equivalent) within the function and sometimes is not.

(simple) if a naked pointer is sometimes checked for air defense and sometimes not checked before dereferencing, an error is reported.

(Simple) Warn if a not_null pointer is tested against nullptr within a function.

(simple) if the not_null pointer makes an air defense judgment inside the function, give an alarm.

Thank you for your reading, the above is the content of "Why do you use not_null in C++ to indicate that null is invalid value". After the study of this article, I believe you have a deeper understanding of why C++ uses not_null to indicate that null is an invalid value, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report