these two pieces of example code do the same job, and both make sense to me. So when should I use concept and when to use interface?
IMHO using interface requires to explicitly create the abstract class and inherit from it, what's the necessity to use interface?
might be a silly question but I'm really curious.
template<class T>
concept HasVolume =
requires(T t){{ t.volume() }; };
template<HasVolume V>
int PrintVolumeConcept(V v)
{
cout << v.volume() << endl;
return v.volume();
}
class Cube
{
public:
int side;
int volume() const
{ return side * side * side; }
};
class HasVolume
{
public:
virtual int volume() const = 0;
};
class Cube : public HasVolume
{
public:
int side;
int volume() const
{ return side * side * side; }
};
int PrintVolume(const HasVolume& v)
{
cout << v.volume() << endl;
return v.volume();
}