您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。