Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.

Concepts and External Concepts

Generic programming in C++ is characterized by the use of function and class templates where the template parameter(s) must satisfy certain requirements.Often these requirements are so important that we give them a name: we call such a set of type requirements a concept. 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. The concept can be specified as a set of member functions with well-defined semantics and a set of nested typedefs with well-defined properties.

Often it much more flexible to provide free-standing functions and typedefs which provides the exact same semantics (but a different syntax) as specified by the concept. This allows generic code to treat different types as if they fulfilled the concept. In this case we say that the concept has been externalized or that the new requirements constitutes an external concept . We say that a type conforms to an external concept or that it is a model of an external concept . A concept may exist without a corresponding external concept and conversely.

Whenever a concept specifies a member function, the corresponding external concept must specify a free-standing function of the same name, same return type and the same argument list except there is an extra first argument which must be of the type (or a reference to that type) that is to fulfill the external concept. If the corresonding member function has any cv-qulifiers, the first argument must have the same cv-qualifiers. Whenever a concept specifies a nested typedef, the corresponding external concept specifies a type-generator, that is, a type with a nested typedef named type. The type-generator has the name as the nested typedef with _of appended. The converse relationship of an external concept and its corresponding concept also holds.

Example:

A type T fulfills the FooConcept if it has the follwing public members:

void T::foo( int ) const;
int T::bar();
typedef implementation defined foo_type;

The corresponding external concept is the ExternalFooConcept.

A type T fullfills the ExternalFooConcept if these free-standing functions and type-generators exists:

void foo( const T&, int );
int bar( T& );
foo_type_of< T >::type;



Literature


© Thorsten Ottosen 2003-2004 (nesotto_AT_cs.auc.dk). Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.