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

Why does C + avoid concepts that have no clear semantics?

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

Share

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

This article introduces the knowledge of "why C+ avoids defining concepts without clear semantics". Many people will encounter such a dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

T.20: avoid defining "concepts" without clear semantics

Reason (reason)

Concepts are meant to express semantic notions, such as "a number", "a range" of elements, and "totally ordered." Simple constraints, such as "has a + operator" and "has a > operator" cannot be meaningfully specified in isolation and should be used only as building blocks for meaningful concepts, rather than in user code.

The concept is intended to express some semantic concept, such as "number", "range" of elements, and "complete order". Simple constraints, such as the + operator and the > operator, cannot be counted as independent, explicitly defined, and apply only to the components of a specific concept, rather than directly in the code.

Example, bad (using TS concepts)

Negative example (using the TS concept)

Template

Concept Addable = has_plus; / / bad; insufficient

Template auto algo (const Na, const Nb) / / use two numbers

{

/ /...

Return a + b

}

Int x = 7

Int y = 9

Auto z = algo (x, y); / / z = 16

String xx = "7"

String yy = "9"

Auto zz = algo (xx, yy); / / zz = "79"

Maybe the concatenation was expected. More likely, it was an accident. Defining minus equivalently would give dramatically different sets of accepted types. This Addable violates the mathematical rule that addition is supposed to be commutative: a roomb = = bachela.

Maybe the expected operation is the link. More likely, it was an accident. Defining subtractive operations equivalently will provide a significantly different set of acceptable types. This additivity violates the rule that addition operations satisfy the commutative law (a+b==b+a).

Note (Note)

The ability to specify a meaningful semantics is a defining characteristic of a true concept, as opposed to a syntactic constraint.

The ability to define clear semantics is a clear feature of a real concept, not a syntactic constraint.

Example (using TS concepts)

Example (using TS concepts)

Template

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

Concept Number = has_plus

& & has_minus

& & has_multiply

& & has_divide

Template auto algo (const Na, const Nb)

{

/ /...

Return a + b

}

Int x = 7

Int y = 9

Auto z = algo (x, y); / / z = 16

String xx = "7"

String yy = "9"

Auto zz = algo (xx, yy); / / error: string is not a NumberNote (note)

Concepts with multiple operations have far lower chance of accidentally matching a type than a single-operation concept.

Concepts that contain multiple operations are much less likely to match a type than a concept with only one operation.

Enforcement (implementation recommendations)

Flag single-operation concepts when used outside the definition of other concepts.

Marks single-operation concepts that are outside the scope of other concept definitions.

Flag uses of enable_if that appears to simulate single-operation concepts.

Tag uses enable_if to simulate the concept of a single operation.

This is the end of the content of "Why C+ avoids defining concepts without clear semantics". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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