Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are concepts?

Tags:

I've heard all this new (on /.) about C++0x not having concepts anymore, but I have no idea what they are? Can someone explain to me?

like image 579
devin Avatar asked Aug 10 '09 01:08

devin


1 Answers

Concepts are a generic programming feature which allow someone writing templated code to specify requirements which the type parameters need to meet.

For example, some collection types need for the type parameter for the collection to define the < operator. So the programmer might define a concept called LessThanComparable which tells the compiler that the type parameter to the templated class needs to have operator< defined. If the template user then tries to instantiate the template using a type that does not have the LessThanComparable concept (i.e. does not have an operator< function) the compiler can emit a simple error message rather than the torrent of error messages associated with templated code. The compiler may also be able to take advantage of the extra information provided by concepts to generate more efficient code.

This is something of an oversimplication, but I think it gives you the general idea behind concepts.

If you want to try out some of the capabilities of concepts, take a look at the Boost.Concept Check library. I don't think it provides the full range of capabilities that were going to be in the standard, but it's a good place to start.

You may also want to look at ConceptC++, there's a good definition of concepts there.

like image 55
Ferruccio Avatar answered Sep 27 '22 16:09

Ferruccio