Currently best way I can think of is to use static_assert, but I would prefer nicer way.
#include <set>
#include <forward_list>
using namespace std;
template<typename C>
concept bool SizedContainer = requires (C c){
c.begin();
c.end();
{c.size()} -> size_t;
};
static_assert(SizedContainer<std::set<int>>);
static_assert(!SizedContainer<std::forward_list<int>>);
static_assert(!SizedContainer<float>);
class MyContainer{
public:
void begin(){};
void end(){};
size_t size(){return 42;};
};
static_assert(SizedContainer<MyContainer>);
int main()
{
}
MyContainer
would be incomplete, and so the concept check would likely fail. So I don't see a better way. – Postoperativestatic_assert
not an appropriate way to test this? It seems very clear to me what needs to be checked. And the entirety of a class's conceptual interface is not bound by its definition, so there's no reason to expect such checks to be part of the definition. – Bobettebobina