Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Concepts differ from Interfaces?

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

like image 391
user4812 Avatar asked Jul 25 '09 22:07

user4812


People also ask

What is interface concept?

Interface Concept has developed a product portfolio based on 3U and 6U VPX form-factors. Interface Concept's 6U Front-End Processing (FEP) boards are designed to integrate two FPGAs, a processor and a huge array of communication interfaces offering the integrators with a single-board compact solution.

What is interface explain with example?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".

Is there concept of interface in C++?

In C++ programming there is no built-in concept of interfaces. In order to create an interface, we need to create an abstract class which is having only pure virtual methods. In C++, Interfaces are also called pure abstract classes.

Why do we use interfaces?

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship.


2 Answers

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!

like image 66
Khaled Alshaya Avatar answered Oct 09 '22 09:10

Khaled Alshaya


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.

like image 7
Klaim Avatar answered Oct 09 '22 09:10

Klaim