Concept (generic programming)
What is concept in generic programming
在前文中(wikipedia Generic programming)中,我们已经知道:
fundamental requirements on types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts, with generic functions implemented in terms of these concepts
显然,concept就是对requirement on type的formal description。
NOTE: 参见
Theory\Programming-paradigm\Type-requirement
。
在C++ Type Traits中有这样的描述:
Generic programming -- that is, writing code that works with any data type meeting a set of requirements -- has become the method of choice for delivering reusable code.
这段话引发了我思考一个问题:在generic programming中,如何来描述requirement呢?在维基百科Generic programming的第三段中就给出了这个问题的答案:
fundamental requirements on types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts
即concepts。
concept是generic programming的重要组成部分
Generic programming and design by contact
Requirement其实就是一种contact。
wikipedia Concept (generic programming)
In generic programming, a concept is a description of supported operations on a type, including syntax and semantics. In this way, concepts are related to abstract types but concepts do not require a subtype relationship.
NOTE: "description of supported operations "表明concept是behavior-based,关于behavior-based,参见
./Templates-and-Duck-Typing
。
Language use
C++
In the C++ 1998 standard, the Concept term was introduced to name just a simple description of the requirements for particular type, usually being a template parameter. It was not encoded in the language explicitly – the concept was expressed only by what operations are attempted on objects of that type and what is expected to work (that is, to compile correctly). There was a proposal to add concepts as an explicit language feature in C++11, though it was rejected as "not ready".
Java and C#
As generics in Java and C# have some similarities to C++'s templates, the role of concepts there is played by interfaces.