In the general world of programming / TMP, what is a model / policy and "concept"?

I would like to know the exact but concise definitions of these three concepts in one place. The quality of the response should depend on the following two points.

  • Show a simple code snippet to show how and for what the concept / technology is used.
  • Be simple enough to understand so that a programmer can understand this without any impact on this area.

Note:

There are probably many correct answers, since each concept has many different aspects. If there are a lot of good answers, I will eventually turn the question into CW and summarize the answers.

- Post Accept Edit -

Boost has a nice article on common programming concepts.

+6
c ++ generic-programming templates metaprogramming components
source share
3 answers

A concept is a set of type requirements. For example, you might have a concept called "RandomAccessible" that puts a requirement in a type that it implements operator[](int) O (1) times.

As concepts have been excluded from the upcoming C ++ standard, they exist only as inconspicuously in C ++ as documentation. As an example, you can read the SGI description of the container concept .

When a type meets all the requirements of a concept, you call it a model of that concept. For example, std::vector is a model of the concept of a container (or, equivalently, std::vector of the “Container” models).

Finally, politics is a unit of behavior that can be combined with other units of behavior to create complex classes. For example, suppose you want to create two classes: a fixed-size array and a dynamically resizable array. Both of these classes have many common functions, but they simply differ in storage mechanisms and some functions (for example, you cannot call push_back in a fixed-size array).

 template <class T, class StoragePolicy> class array : public StoragePolicy { public: T& operator[](int i) { return data[i]; } }; template <class T, int N> class fixed_storage { T data[N]; }; template <class T> class dynamic_storage { T* data; public: void push_back(const T& value) { // Code for dynamic array insertion } }; 

Usage will be as follows:

 int main() { array<int, fixed_storage<int, 10> > fixed_array; array<int, dynamic_storage<int> > dynamic_array; dynamic_array.push_back(1); fixed_array[9] = dynamic_array[0]; } 

Obviously, this is a very crude and incomplete example, but I hope that it illuminates the concept of politics.

Note that in this example we can say that fixed_storage and dynamic_storage are “models” of the StoragePolicy concept. Of course, we would need to formally determine what exactly StoragePolicy concepts StoragePolicy from their models. In this case, just define the index variable data .

+8
source share

A concept is a set of requirements that a type must meet to model a concept.

For example, the type T is equal to LessThanComparable if, for a pair of objects a and b type T expression a < b well formed, converts to bool and induces a strict weak ordering relation. The int type is an example of a LessThanComparable model.

Concepts can form refinement hierarchies. Concept a is a refinement of concept b if requirements a are a superset of requirements b . For example, BidirectionalIterator is a refinement of ForwardIterator .

Concepts are used to limit the set of types with which a template can be specialized. For example, the std::sort algorithm may take a couple of objects while they model RandomAccessIterator .

 std::vector<int> vec; std::list<int> list; // OK, std::vector<int>::iterator is a model of `RandomAccessIterator`. std::sort(vec.begin(), vec.end()); // error, std::list<int>::iterator is only a model of `BidirectionalIterator`. std::sort(list.begin(), list.end()); 

Note that concepts are informal objects used in the C ++ standard and various other documents. The language does not support concepts directly ( yet ).

+2
source share

The SGI Ao documentation refers to the “model” as to what was introduced as “concepts” in the C ++ 0x proposal: this is the compilation time equivalent that the “interface” is in OO modeling. It summarizes the requirements that the generic code generates for the template parameter.

As an example, you can say that the OutputIterator parameters of the std::transform function must implement operator++() and operator=( T ) for the function to work.

Politics is another thing: it makes the algorithm volatile from the outside. A good example is the less-used Allocator stl container parameter: it tells the algorithm how to allocate memory. If you want, you can do std::vector<int, AllocateOnCloud> , where all vector functions will allocate memory in the cloud, and not on the heap. (I'm not going to implement this allocator, mind).

+2
source share

All Articles