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 does C++ define axioms for concepts

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 introduces "how C++ defines axioms for concepts". In daily operation, I believe many people have doubts about how C++ defines axioms for concepts. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about how C++ defines axioms for concepts. Next, please follow the editor to study!

T.22: define axioms for concepts

Reason (reason)

A meaningful/useful concept has a semantic meaning. Expressing these semantics in an informal, semi-formal, or formal way makes the concept comprehensible to readers and the effort to express it can catch conceptual errors. Specifying semantics is a powerful design tool.

Meaningful / useful concepts contain semantic meanings. Expressing these semantics in an informal, semi-formal, or formal way can make concepts easier for users to understand, and efforts to express concepts can catch conceptual errors. Definition semantics is a powerful design tool.

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

Template

/ / The operators +, -, *, and / for a number are assumed to follow the usual mathematical rules

/ axiom (T a, T b) {a + b = = b + a; a-a = = 0; a * (b + c) = = a * b + a * c; / *. * /}

Concept Number = requires (T a, T b) {

{a + b}-> T; / / the result of a + b is convertible to T

{a-b}-> T

{a * b}-> T

{a / b}-> T

}

Note (Note)

This is an axiom in the mathematical sense: something that may be assumed without proof. In general, axioms are not provable, and when they are the proof is often beyond the capability of a compiler. An axiom may not be general, but the template writer may assume that it holds for all inputs actually used (similar to a precondition).

This is an axiom about the laws of mathematics: certain hypotheses that do not require evidence. In general, axioms are unprovable, and even if they can be proved, they usually exceed the capabilities of the compiler. The axiom may not be universal, but the template author can assume that it is valid for all inputs actually used (similar prerequisites)

Note (Note)

In this context axioms are Boolean expressions. See the Palo Alto TR for examples. Currently, C++ does not support axioms (even the ISO Concepts TS), so we have to make do with comments for a longish while. Once language support is available, the / / in front of the axiom can be removed

In this context, an axiom is a Boolean expression. See the example in Palo Alto TR. C++ currently doesn't support axioms (including ISO Concepts TS), so we have to put it in comments for a long time. Once the language provides support for axioms, the preceding / / can be removed.

Note (Note)

The GSL concepts have well-defined semantics; see the Palo Alto TR and the Ranges TS.

The GSL concept provides well-defined semantics. See Palo Alto TR and range TS.

Exception (using TS concepts) (exception (using TS concept))

Early versions of a new "concept" still under development will often just define simple sets of constraints without a well-specified semantics. Finding good semantics can take effort and time. An incomplete set of constraints can still be very useful:

Early versions of new "concepts" that are still under development are usually just simple sets of constraints that may not have a well-defined semantics. It takes effort and time to find perfect semantics. An incomplete set of constraints can also be useful.

/ / balancer for a generic binary tree

Template concept bool Balancer = requires (Node* p) {

Add_fixup (p)

Touch (p)

Detach (p)

}

So a Balancer must supply at least thee operations on a tree Node, but we are not yet ready to specify detailed semantics because a new kind of balanced tree might require more operations and the precise general semantics for all nodes is hard to pin down in the early stages of design.

So the Balancer on the tree node must support at least three operations, but we are not ready to define the semantic details, because the new kind of balanced tree may require more operations, and the accurate, common semantics that apply to all nodes are difficult to determine in the early stages of design.

A "concept" that is incomplete or without a well-specified semantics can still be useful. For example, it allows for some checking during initial experimentation. However, it should not be assumed to be stable. Each new use case may require such an incomplete concept to be improved.

Incomplete or poorly defined "concepts" are still useful. For example, it allows some checks during the initialization phase. However, it should not be considered stable. Each new usage may change this incomplete concept.

Enforcement (implementation recommendations)

Look for the word "axiom" in concept definition comments

"axiom" is found in the comments of the concept definition.

At this point, the study of "how C++ defines axioms for concepts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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