How do concepts differ from interfaces?

How do concepts (for example, those recently removed from the C ++ 0x standard) differ from interfaces in languages ​​such as Java?

+15
c ++ generics c ++ 11 templates c ++ - concepts
Jul 25 '09 at 22:14
source share
4 answers

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]; // just an example don't take it seriously :) // elements MUST have size member function! }; 

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(/* some condition*/) 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(/* 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 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.

+21
Jul 25 '09 at 10:17
source share

Concepts like types (classes) for templates: this is for the general part of programming only a language.

Thus, it should not replace interface classes (assuming that you mean abstract classes or another equivalent implementation of C ++ for C # or Java interfaces), since it is intended only for checking the types used in the template parameters, in accordance with specific requirements. Type checking is performed only at compile time, as well as for generating the template code, and the interface classes affect the execution.

+6
Jul 25 '09 at 10:18
source share

This is more or less a difference in point of view. Although the interface (as in C #) is specified similarly to the base class, the concept can also be automatically matched (similar to the duck type in Python). It is still unclear at what level C ++ will support automatic matching of concepts, which is one of the reasons why they abandoned it.

+1
Jul 25 '09 at 22:31
source share

Concepts are implicit interfaces. In C # or Java, a class must explicitly implement an interface, whereas in C ++ a class is part of the concept only as long as it satisfies the limitations of the concept.

The reason you see concepts in C ++ rather than Java or C # is because C ++ does not actually have β€œinterfaces”. Instead, you can simulate an interface using multiple inheritances and abstract base classes without elements. This is a bit of a hack and can be a headache for work (for example, virtual inheritance and The Diamond Problem ). Interfaces play a critical role in OOP and polymorphism, and this role has not yet been adequately performed in C ++. Concepts are the answer to this problem.

0
Aug 31 '17 at 4:59 on
source share



All Articles