Concept (generic programming)
Encyclopedia
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 base classes but concepts do not require a subtype relationship.
, as this was one of the first libraries that extensively used templates.
Primarily (in 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 never encoded in the language explicitly - the concept was expressed only by what operations are tried to be performed on objects of that type and what is expected to work (that is, to compile correctly).
As generics in Java and C# have some similarities to C++'s templates, the role of concepts there is played by interfaces. 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 by two ways:
Another language implementing something very similar to concepts is Haskell
, where the feature is called type classes
.
Generic programming
In a broad definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...
, a concept is a description of supported operations on a type, including syntax and semantics. In this way, concepts are related to abstract base classes but concepts do not require a subtype relationship.
Languages using
The term was in use as early as 1998 for STLStandard Template Library
The Standard Template Library is a C++ software library which later evolved into the C++ Standard Library. It provides four components called algorithms, containers, functors, and iterators. More specifically, the C++ Standard Library is based on the STL published by SGI. Both include some...
, as this was one of the first libraries that extensively used templates.
Primarily (in 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 never encoded in the language explicitly - the concept was expressed only by what operations are tried to be performed on objects of that type and what is expected to work (that is, to compile correctly).
As generics in Java and C# have some similarities to C++'s templates, the role of concepts there is played by interfaces. 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 by 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
Another language implementing something very similar to concepts is Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
, where the feature is called type classes
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...
.
Example
For example, if a typeI
satisfies the Trivial Iterator concept in C++, and i
is of type I
, the following are valid expressions with corresponding semantics:
-
I i
default construction. -
*i
must be convertible to some typeT
. -
i->m
is valid if(*i).m
is.