The concepts are intended for polymorphism of compilation time, which means parametric general code. Interfaces are designed for run-time polymorphism.
You must implement the interface when implementing the Concept. The difference is that you do not need to explicitly indicate that you are implementing the concept. If the appropriate interface is consistent, then there is no problem. In the case of interfaces, even if you have implemented all the required functions, you must excite that you implement it!
I will try to clarify my answer :)
Imagine that you are designing a container that accepts any type that has member function size . We formalize the concept and call it HasSize, of course, we must define it elsewhere, but this is no more than an example.
template <class HasSize> class Container { HasSize[10];
Then imagine that we instantiate our Container and call it myShapes , Shape is the base class and defines the size . > member function. The Square and the Circle are just children. If the form did not determine the size, then an error must be created.
Container<Shape> myShapes; if() myShapes.add(Square()); else myShapes.add(Circle());
I hope you see that at compile time Shape can be checked for HasSize , there is no reason to do a check at runtime. Unlike myShapes elements, we could define a function that controls them:
void doSomething(Shape* shape) { if()
In this function, you cannot know what will be transferred before the execution of Circle or Square!
These are two tools for similar work, although the interface or whatever you call it can do almost the same Concepts work at runtime, but you lose all the benefits of checking time and optimizing compilation time.
AraK Jul 25 '09 at 10:17 2009-07-25 22:17
source share