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.
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, the primary designer of the STL.
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
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
, though it was rejected as "not ready".
C++20 eventually accepted the refined design of concept. Concepts are an example of
structural typing.
As generics in
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
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. This is known as
nominal typing. 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.)
The
Nim programming language implements concepts as a series of arbitrary compile-time boolean predicates.
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 pioneered several programming language ...
, where the feature is called
type classes.
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:
#include
template
requires std::totally_ordered
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)
* Concepts (C++)
* Interface (Java)
* Type class
References
External links
Boost Generic Programming Techniques
* Douglas Gregor, 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
{{Webarchive, url=https://web.archive.org/web/20170809215622/http://sms.cs.chalmers.se/publications/papers/2008-WGP.pdf , date=2017-08-09
Generic programming
ja:コンセプト (C++)