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

How to avoid complementary constraints in C++

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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!

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