views:

300

answers:

3

How do Concepts (ie those recently dropped from the C++0x standard) differ from Interfaces in languages such as Java?

+11  A: 

Concepts are for compile-time polymorphism, That means parametric generic code. Interfaces are for run-time polymorphism.

You have to implement an interface as you implement a Concept. The difference is that you don't have to explicitly say that you are implementing a Concept. If the required interface is matched then no problems. In the case of interfaces, even if you implemented all the required functions, you have to excitability say that you are implementing it!


I will try to clarify my answer :)

Imagine that you are designing a container that accepts any type that has the size member function. We formalize the Concept and call it HasSize, of course we should define it elsewhere but this is an example no more.

template <class HasSize>
class Container
{
  HasSize[10]; // just an example don't take it seriously :)
 // elements MUST have size member function!
};

Then, Imagine we are creating an instance of our Container and we call it myShapes, Shape is a base class and it defines the size member function. Square and Circle are just children of it. If Shape didn't define size then an error should be produced.

Container<Shape> myShapes;

if(/* some condition*/)
    myShapes.add(Square());
else
    myShapes.add(Circle());

I hope you see that Shape can be checked against HasSize at compile time, there is no reason to do the checking at run-time. Unlike the elements of myShapes, we could define a function that manipulates them :

void doSomething(Shape* shape)
{
    if(/* shape is a Circle*/)
     // cast then do something with the circle.
    else if( /* shape is a Square */)
     // cast then do something with the square.
}

In this function, you can't know what will be passed till run-time a Circle or a Square!

They are two tools for a similar job, though Interface-or whatever you call them- can do almost the same job of Concepts at run-time but you lose all benefits of compile-time checking and optimization!

AraK
Could the downvoter explain what is wrong with my answer ?
AraK
Ya, this answer seems fine to me. +1
GMan
I was the downvoter. AraK has completely re-edited his answer, but the re-edit doesn't show up because of the vagaries of SO.
anon
Sorry I was writing in hurry I didn't finish my post first time :(
AraK
@AraK, I changed "excitability" to "explicitly" - is this what you mean? (it didn't make sense before)
Jesse Beder
yes, Thanks Jesse :)
AraK
+1 better formulation than my answer :)
Klaim
+7  A: 

Concepts are likes types (classes) for templates: it's for the generic programming side of the language only.

In that way, it's not meant to replace the interface classes (assuming you mean abstract classes or other C++ equivalent implementation of C# or Java Interfaces) as it's only meant to check types used in template parameters to match specific requirements. The type check is only done at compile time like all the template code generation and whereas interface classes have an impact on runtime execution.

Klaim
+2  A: 

It's more or less a difference in the point of view. While an interface (as in C#) is specified similar to a base class, a concept can also be matched automatically (similar to duck-typing in Python). It is still unclear to which level C++ is going to support automatic concept matching, which is one of the reasons why they dropped it.

Malte Clasen