Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to avoid complementary constraints in C++

Shulou Source: shulou.com Published: 2022-06-01 20:48:57 10月04日 Update

This article mainly explains "how C++ can avoid complementary constraints". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how C++ avoids complementary constraints".

T.25: avoid complementary constraints

Reason (reason)

Clarity. Maintainability. Functions with complementary requirements expressed using negation are brittle.

Clarity. Maintainability. Functions that contain complementary requirements expressed in a negative way are fragile.

Example (using TS concepts) (example (using TS concept))

Initially, people will try to define functions with complementary requirements:

At first, people tried to define functions in terms of complementary requirements:

Template

Requires! C / / bad

Void f ()

Template

Requires C

Void f ()

This is better:

The following code is better:

Template / / general template

Void f ()

Template / / specialization by concept

Requires C

Void f ()

The compiler will choose the unconstrained template only when C is unsatisfied. If you do not want to (or cannot) define an unconstrained version of f (), then delete it.

The compiler chooses an unconstrained template only if C is not satisfied. If you don't want (or can't) set the non-binding version of f (), delete it.

Template

Void f () = delete

The compiler will select the overload and emit an appropriate error.

The compiler chooses to overload and report the appropriate error.

Note (Note)

Complementary constraints are unfortunately common in enable_if code:

Unfortunately, complementary constraints are common in enable_if code:

Template

Enable_if, void > / / bad

F ()

Template

Enable_if

F ()

Note (Note)

Complementary requirements on one requirements is sometimes (wrongly) considered manageable. However, for two or more requirements the number of definitions needs can go up exponentially (2, 4, 8, 16,...):

A complementary requirement on a requirement is sometimes (mistakenly) considered controllable. However, for two or more requirements, the defined number needs to grow exponentially.

C1 & C2

! C1 & & C2

C1 &! C2

! C1 & &! C2

Now the opportunities for errors multiply.

Now the possibility of making a mistake has also doubled.

Enforcement (implementation recommendations)

Flag pairs of functions with C and! C constraints

The tag uses C and! function pairs with C constraints.

Thank you for your reading. the above is the content of "how to avoid complementary constraints on C++". After the study of this article, I believe you have a deeper understanding of the problem of how to avoid complementary constraints on C++. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Requirements complementarities Clearing functions errors learning code content methods more compilers selections appropriateness fragility unfortunately two people reasons only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Xiaomi NVidia macOS