C++ Blog

C++ Concept Part 3

Posted in c++0x, templates by Umesh Sirsiwal on January 25, 2009

This is the last post in series of posts on C++0x concepts. In this we will look at C++0x concepts. The C++0x standard for concepts is almost identical to ConceptC++. The syntax are almost identical. Significant parts of ConceptC++ have become part of the draft standard.

One of the things we have not looked at the definition of concept map. In the previous example of sort, the template takes ForwardIterator as an argument. The article did not discuss what makes a forward iterator. Can we use an int* as a forard iterator? We have also said that ForwardIterator must has value_type argument. int* does not have a member value_type. But we want to pass int* as parameter to sort so that for example, we can sort an array of integers.

C++0x provides a way to solve this problem called concept_map. Using concept_map we can define what constitute ForwardIterator.

template ;
	concept_map ForwardIterator {
                                    // T*'s value_type is T
		typedef T value_type;
	};

Using above syntax we are saying that any T”*’s value_type is T. This removes need to wrap int* in another container.

In all C++0x concept provides solution for one of the biggest ongoing frustration with generic programming with C++. It will provide early and clear compilation errors for template users and authors both and bind them in template parameter contract.

Other C++0x sources on the web

Leave a comment