Posted on 2007-05-31 14:45
chemz 閱讀(2184)
評論(6) 編輯 收藏 引用 所屬分類:
C++
什么是concept
在范型程序設(shè)計領(lǐng)域有一個必須要掌握的名詞“concept”,中文翻譯叫做“概念”,這個
翻譯僅僅是一個concept英文的直譯,并沒有包含范型程序設(shè)計中concept的特定含義,要想
真正的進(jìn)入到范型程序設(shè)計領(lǐng)域,理解范型設(shè)計的思想,首先必須完全弄明白什么是concept?
在《美國傳統(tǒng)詞典[雙解]》中,concept這一名詞被解釋為:
1. A general idea derived or inferred from specific instances or occurrences.
概念:從特定情形或事件中得到或推斷出的一般性想法
2. Something formed in the mind; a thought or notion.See Synonyms at idea.
想法:在腦海中形成的想法;思想或觀念參見 idea
3. Usage Problem A scheme; a plan.
設(shè)想;計劃
在范型程序設(shè)計的發(fā)源地SGI,在STL_DOC中有對什么是concept的專門的描述:
“One very important question to ask about any template function, not just
about STL algorithms, is what the set of types is that may correctly be
substituted for the formal template parameters. Clearly, for example,
int* or double* may be substituted for find's formal template parameter
InputIterator. Equally clearly, int or double may not: find uses the
expression *first, and the dereference operator makes no sense for an
object of type int or of type double. The basic answer, then, is that
find implicitly defines a set of requirements on types, and that it may
be instantiated with any type that satisfies those requirements. Whatever
type is substituted for InputIterator must provide certain operations: it
must be possible to compare two objects of that type for equality, it must
be possible to increment an object of that type, it must be possible to
dereference an object of that type to obtain the object that it points to,
and so on.
Find isn't the only STL algorithm that has such a set of requirements; the
arguments to for_each and count, and other algorithms, must satisfy the same
requirements. These requirements are sufficiently important that we give them
a name: we call such a set of type requirements a concept, and we call this
particular concept Input Iterator. We say that a type conforms to a concept,
or that it is a model of a concept, if it satisfies all of those requirements.
We say that int* is a model of Input Iterator because int* provides all of the
operations that are specified by the Input Iterator requirements.
Concepts are not a part of the C++ language; there is no way to declare a
concept in a program, or to declare that a particular type is a model of a
concept. Nevertheless, concepts are an extremely important part of the STL.
Using concepts makes it possible to write programs that cleanly separate
interface from implementation: the author of find only has to consider the
interface specified by the concept Input Iterator, rather than the
implementation of every possible type that conforms to that concept. Similarly,
if you want to use find, you need only to ensure that the arguments you pass
to it are models of Input Iterator. This is the reason why find and reverse
can be used with lists, vectors, C arrays, and many other types: programming
in terms of concepts, rather than in terms of specific types, makes it possible
to reuse software components and to combine components together. ”
侯捷在《Genericity/STL 大系》一文中將concept解釋為:(此處和SGI的說法吻合)
“所謂 concept,描述某個抽象型別的條件(或說需求,requirements)。concept 并
不是一個 class,也不是一個變數(shù)或是一個 template 叁數(shù);C++ 語言之中沒有任何東
西可以直接代表一個concept。然而,在每一個用到泛型程式設(shè)計方法的 C++ 程式中,
concept 非常重要。由 concepts 所構(gòu)成的階層體系,正是 STL 的主體結(jié)構(gòu)。
當(dāng)某個型別滿足某個 concept 的所有條件,我們便說此型別是該 conecpt 的一個model。
concept 可被視為一組型別條件。如果型別 T 是 concept C 的一個 model,那麼 T 就
一定滿足 C 的所有條件。因此,concept 亦可被視為是一組型別。如果型別 T 是
concept C 的一個 model,我們便可說 T 隸屬於「C 所表現(xiàn)的一組型別」”
由上面的三種不同角度的解釋可以看出來,concept實際上應(yīng)該是一個人在認(rèn)識一種事物
或現(xiàn)象的過程中總結(jié)或抽象出來的一種思想和設(shè)計,而由于人類在認(rèn)知事物、現(xiàn)象和世界時
均帶有自身的約束和局限,那么就決定了總結(jié)或抽象出來的思想和設(shè)計都具有一定的約束和
局限,或者叫做具有一定的邊界(范圍)。那么在人們利用這些思想或設(shè)計重新審視一種
事物或現(xiàn)象時,就會在這種思想和設(shè)計的邊界之內(nèi)來進(jìn)行判斷,在這個范圍內(nèi)的我們稱之為
符合這種概念,那么如何判斷一種事物或現(xiàn)象是否符合某個概念呢?一般情況下必須在這個
concept下形成特殊的判斷條件,這些條件組合起來完成了一種concept和另外一種concept
之間的區(qū)別,也就是條件的集合稱為了判斷一種事物或現(xiàn)象是否歸屬于某一個concept的依據(jù)。
當(dāng)然,不同的concept之間可能會有交集,但絕對不會完全重合。
上面是一種一般性的極度抽象的解釋concept的方法,在將這種解釋應(yīng)用到某一個實際的
真實環(huán)境或領(lǐng)域時應(yīng)該是如何的呢?我們需要給這個concept指定一個名稱,用于表示這個
concept,同時我們還必須確定這個用于判斷一種事物或現(xiàn)象是否歸屬于這個concept的條件
,進(jìn)而形成concept的判斷邊界(范圍,集合)。那么在范型程序設(shè)計中的concept就非常好
理解了,比如:Iterator這樣的一種concept,同樣是在人們的程序設(shè)計過程中為了完成對
容器對象中的元素進(jìn)行訪問,而抽象和總結(jié)出來的處理類似問題域的一種思想或設(shè)計,那么
經(jīng)過更進(jìn)一步的設(shè)計和構(gòu)想,就會逐漸的將什么才能夠算得上是一個iterator的判斷依據(jù)
確定下來,形成Iterator這一concept的邊界,也就是他形成了一些用于鑒別一個實例是否
歸屬于一個concept范疇的條件集合(約束集),在軟件中這實際上就是一個實例必須具備
concept所定義的接口形式和語義(包括編譯時和運(yùn)行時),在Iterator概念中就必須明確
的要求一個實例必須提供類似于++、--、*、==、!=等等這樣的接口形式和每一種接口所代
表的語義,要注意形式和語義是缺一不可的,必須嚴(yán)格的符合,否則就不能夠算是符合某一
個concept。
其實concept就是這些東西,上面的解釋過于形式化,不太好理解。因為不是每一個都能
很好的僅僅通過形式化的定義理解一個concept的形成條件、最初的問題域、和提出者的思想
的(也就和一個代數(shù)公式一樣,必須要在他所形成和適用的環(huán)境中才能夠較容易的理解),這
樣一來,人們會在concept形式化的描述中添加一些該concept最初的問題域的描述、形成時
提出者的思想和初衷想法等,以幫助理解這個concept,所以就形成了STL_DOC和侯捷等其他人
的有關(guān)concept就是一組需求的解釋,其實STL_DOC的解釋更為全面和準(zhǔn)確。