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

If there is a premise in C++, why should it be explained?

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 introduces "Why must explain if there is a premise in C++". In daily operation, I believe that many people have doubts about why they must explain if there is a premise in C++. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the doubt that "why must be explained if there is a premise in C++?" Next, please follow the editor to study!

Reason (reason)

The meaning of the parameter may limit its use in the callee.

Example (sample)

Consider: consider:

Double sqrt (double x)

Where x must be non-negative. The type system cannot express this (simply and naturally), so we have to use other ways. For example

Double sqrt (double x); / / x must be nonnegative

Some prerequisites can be expressed with assertions. For example:

Double sqrt (double x) {Expects (x > = 0); / *... * /}

Ideally, this Expects (x > = 0) should be part of the sqrt interface, but this is not easy to implement. So far, we have put it in the definition (function body).

Note (Note)

Formally defining requirements like Expects (p) is a better choice. If it is not possible, you can use English annotations, such as

/ / the sequence [PRAUQ) is ordered using

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