Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 19:12:10 10月03日 Update

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

Tags: Premise function Clearing Learning member condition more help practical effective next this is not reason parameter meaning object situation interface article way Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Redmi Xiaomi Shulou Information