I finally got around to reading Stroustrup’s “The design of C++0x”, and was impressed by Stroustrup’s sense of language design, especially the clarity of thought with which he presents the topics.

I still haven’t understood the part about “concepts” though. I wonder how it is different from interfaces (in Java).

7 thoughts on “Concepts

  1. If interfaces are used, the class when coded has to be specified with its interfaces. Then, it isn’t Generics anymore and everything that is good about templates in C++ is lost.

    Here, the idea is not to worry at all about having to implement any interfaces or derive from anything for classes that will be potential arguments to templates.
    It’s only when the templates are instantiated by passing in these classes as arguments that ‘Concepts’ come into the picture. (Of course, concepts also needs to be specified when templates are defined.)

  2. C++ has had latent typing for a long time. That’s what templates provide, right?

    You’ve seen those error messages when a template is instantiated and the compiler realises that operations that a template expects of its arguments don’t exist? They are pretty long cryptic messages because what the compiler did was just substitute the template arguments in the template and then realise that things don’t make sense.

    If concepts are specified (set of methods that a class must have to be used as a template argument) for a template, then

    1) Error messages will be a lot more easy to read because all it will say is that the template argument doesn’t satisfy a particular concept because of missing methods.

    2) Concepts allows us to compile code that uses templates without having to compile _with_ the template code. Just knowing what concepts are required for a class to qualify is enough.

    The point is that the compiler doesn’t have to patch all those template arguments into the template and then go “Oops” :) It can get pretty darn crytic when templates are used to its full potential. Template arguments call methods on each other in the template or are passed to other class which make use of them, for example.

    And again, unlike interfaces, no class has to define what concepts it satisfies. If it has three methods, it can satisfy seven different concepts. And this checking happens only during template instantiation time.

  3. @Gavri: Woah, thanks for taking the time to explain this so neatly. It makes a lot more sense now.

    Once I read up more, I’ll probably ping you off-blog :-)

  4. Actually, this is all I know. I took the time to find out what concepts mean because of C++ nostalgia. Ugly language, but that’s what makes it so much fun :D

  5. Here are two links which should help you:

    A concept is an abstract notion. A description of a type which describes the signature and the declared member types. The main difference between a concept and an interface is that a concept specifies that a model must provide member types with a specific name, but not what that type is.

    For instance a Container concept says that a Container must have a local typedef named value_type, but this type can be anything.

Comments are closed.