Concept (generic Programming)
   HOME

TheInfoList



OR:

In
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
, a concept is a description of supported operations on a type, including syntax and semantics. In this way, concepts are related to
abstract type In programming languages, an abstract type is a type in a nominative type system that cannot be instantiated directly; a type that is not abstract – which ''can'' be instantiated – is called a ''concrete type''. Every instance of an abstrac ...
s but concepts do not require a subtype relationship.


Language use

The term was in use as early as 1998 for STL, as this was one of the first libraries that extensively used templates. The term ''concept'' (and its popularization) is credited to
Alexander Stepanov Alexander Alexandrovich Stepanov (russian: Алекса́ндр Алекса́ндрович Степа́нов; born November 16, 1950, Moscow) is a Russian-American computer programmer, best known as an advocate of generic programming and as th ...
, the primary designer of the STL. In the
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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 Concepts are defined as abstract ideas. They are understood to be the fundamental building blocks of the concept behind principles, thoughts and beliefs. They play an important role in all aspects of cognition. As such, concepts are studied by sev ...
as an explicit language feature in
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
, though it was rejected as "not ready".
C++20 C++20 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++20 replaced the prior version of the C++ standard, called C++17. The standard was technically finalized by WG21 at the meeting in Prague in February 2020, a ...
eventually accepted the refined design of concept. As generics in
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and C# have some similarities to C++'s
templates Template may refer to: Tools * Die (manufacturing), used to cut or shape material * Mold, in a molding process * Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs Co ...
, the role of concepts there is played by
interfaces Interface or interfacing may refer to: Academic journals * Interface (journal), ''Interface'' (journal), by the Electrochemical Society * ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Lin ...
. However, there is one important difference between concepts and interfaces: when a template parameter is required to implement a particular interface, the matching type can only be a class that implements (explicitly) that interface. Concepts bring more flexibility because they can be satisfied in two ways: * explicitly defined as satisfied by using a concept map (defined separately to the type itself, unlike interfaces) * implicitly defined for "auto concepts", which can be used also for built in types and other types that were not predestined for this use But the C# language has several constructs where the used type does not need to explicitly implement a defined interface, it is only required to match the respective pattern (however, these patterns are not called ''concepts''). E.g. the foreach iteration statement allows the iterated object to be of any type, as long as it implements an appropriate GetEnumerator method. (Compare with the using statement which requires the resource to implement the System.IDisposable interface.) Another language implementing something very similar to concepts is
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
, where the feature is called
type class In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...
es.


Examples


Total ordering

The Total ordering concept describes the semantics of the < operator. A type is totally ordered when < is a binary predicate and satisfies the following properties: * anti-reflexive: !(a < a) for any value a. * transitive: If a < b and b < c then a < c. * anti-symmetric: If a < b then !(b < a). * total: If a != b then a < b or b < a. Many algorithms rely on these properties to function properly. For example the min function can be safely defined on Totally Ordered types: // requires: T satisfies Totally Ordered template T min(T a, T b)


Iterator

If a type I satisfies the Trivial Iterator concept in C++, and i is of type I, the following are valid expressions with corresponding semantics:Trivial Iterator
/ref> * I i default construction. * *i must be convertible to some type T. * i->m is valid if (*i).m is.


See also

*
Protocol (object-oriented programming) In object-oriented programming, an interface or protocol type is a data type describing a set of method signatures, the implementations of which may be provided by multiple classes that are otherwise not necessarily related to each other. A class ...
*
Concepts (C++) Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, fu ...
*
Interface (Java) An interface in the Java programming language is an abstract type that is used to describe a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method sig ...
*
Type class In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...


References

{{Reflist


External links


Boost Generic Programming Techniques
*
Douglas Gregor Douglas may refer to: People * Douglas (given name) * Douglas (surname) Animals * Douglas (parrot), macaw that starred as the parrot ''Rosalinda'' in Pippi Longstocking *Douglas the camel, a camel in the Confederate Army in the American Civil ...
, et al.
Concepts: Linguistic Support for Generic Programming in C++
'
Doug Gregor talk on Concepts at Google
(video)
A comparison of C++ concepts and Haskell type classes
Generic programming ja:コンセプト (C++)