这篇文章主要介绍“C++怎么为概念定义公理”,在日常操作中,相信很多人在C++怎么为概念定义公理问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么为概念定义公理”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
T.22:为概念定义公理
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.
有意义/有用的概念会包含语义上的含义。以非正规的,半正规的或者正规的方式进行表现这些语义可以让概念更容易被用户理解,而且表达概念的努力可以捕捉概念方面的错误。定义语义是一种有力的设计工具。
Example (using TS concepts)(示例(适用TS概念))
template<typename T>
// 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;
}
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).
这是一条有关数学规律的公理:某些不需要证据的假设。通常,公理是不可证明的,即使它们可以证明,通常也会超越编译器的能力。公理可能并不普遍,但是模板作者可以假设它对所有实际使用的输入有效(类似前提条件)
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
在这个上下文中公理是一个布尔类型的表达式。参见Palo Alto TR中的例子。目前C++还没有支持公理(包括ISO Concepts TS),因此我们必须在很长一段时间将它放在注释内。一旦语言提供了对公理的支持,就可以去掉前面的//。
Note(注意)
The GSL concepts have well-defined semantics; see the Palo Alto TR and the Ranges TS.
GSL概念提供了定义良好的语义。参见Palo Alto TR和范围TS。
Exception (using TS concepts)(例外(使用TS概念))
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:
仍在开发中的新“概念”的早期版本通常只是定义某些约束的简单集合,而这些约束可能并不具有良好定义的语义。发现完美的语义需要努力和时间。约束的不完全集合同样可以非常有用。
// balancer for a generic binary tree
template<typename Node> 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.
因此树节点上的Balancer必须至少支持三个操作,但是我们还没有准备好定义语义的细节,因为新种类的平衡树可能需要更多的操作,而且适用于所有节点的准确、通用的语义很难在设计的早期阶段确定。
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.
不完全或者没有良好定义的“概念”仍然有用。例如,它允许在初始化阶段进行某些检查。然而,它不应该被认定是稳定的。每一次新用法都可能让这个不完全的概念发生改变。
Enforcement(实施建议)
Look for the word "axiom" in concept definition comments
在概念定义的注释中发现”axiom“。
到此,关于“C++怎么为概念定义公理”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。