Defining Interfaces in C++: Concepts Versus Inheritance -- Daniel Lemire
In a previous blog post, Daniel Lemire showed how you could define ‘an interface’ in C++ using concepts and stated that he did not take into account inheritance as a strategy. He does so here.
Defining Interfaces in C++: Concepts Versus Inheritance
by Daniel Lemire
From the article:
In a previous blog post, I showed how you could define ‘an interface’ in C++ using concepts. For example, I can specify that a type should have the methods has_next, next and reset:
template <typename T>
concept is_iterable = requires(T v) {
{ v.has_next() } -> std::convertible_to<bool>;
{ v.next() } -> std::same_as<uint32_t>;
{ v.reset() };
};

Every year we run Pure Virtual C++: a free one-day virtual conference for the whole C++ community. Next month we’re doing it again!
A classic, over a decade old and worth making the rounds again: