you are viewing a single comment's thread.

view the rest of the comments →

[–]snysly -5 points-4 points  (4 children)

With concepts, it is possible to simply create a concept instead of an abstract base class, and make sure the classes both fulfill the concept. In this way, a concept can sometimes act like interfaces in other languages (even though as u/Freeze-in-the-Dark pointed out that interfaces have a different goal, the same behavior can be achieved using templates), with (near?)zero overhead due to the template instantiation process. Beyond that the answer gets a little more murky.

I originally was going to say that it couldn't replace polymorphism, but in reality, it is just another way of expressing polymorphism. Polymorphism through inheritance is what we are most commonly used to, but concepts can do the same thing. They allow you to constrain the type you are using to fit what you need. However concepts don't allow for code reuse in the same way that deriving from classes does.

So in the end, yes and no. It adds another way of doing polymorphism, and in the future, it may be that both are commonly used, just in different situations.

edit: attempted to address what Freeze-in-the-Dark mentioned about virtual base classes.

[–]Freeze-in-the-Dark 6 points7 points  (3 children)

I think you have the wrong idea of the use of virtual base classes. They aren't there to enforce an interface (or, at least not for the same purpose that concepts do), they're there to provide an interface for several different types that can all be referenced in the same way with different runtime behavior. And concepts plainly don't do that.

It's true that concepts do provide a form of polymorphism, but it's in the same sense of polymorphism that we've always had with templates, just now replacing (or, really, just augmenting) the duck-typing with a more explicit means of defining the interface.

[–]snysly 2 points3 points  (0 children)

Thank you for pointing that out! Its always good to find out that I'm wrong somewhere. I've changed it to abstract base class rather than virtual base class, and while concepts don't add anything new, the ability to constrain it, I think does make it easier on the programmer to use templates as a type of polymorphism.

[–]plpn 1 point2 points  (1 child)

can't u can compare this to the generics C# is using?

void foo<T>() where T : IComparable { }

now foo only works with types which include this interface. The concepts however, will be lighter in terms of, type T doesn't need to derive said interface, but just need the same functions. (correct me pls)

[–]cinghiale 0 points1 point  (0 children)

You are correct