2

How can I programatically implement a concept as if it were an actual "interface"?

For example, I was trying recently to write a contigious iterator for a custom container. And because contigious iterators have a lot of traits, I wanted to make sure I didn't miss anything, so I used a static assert like the following:

struct CustomContainer
{
    struct Iterator
    {
        using iterator_category = std::contiguous_iterator_tag;
        /* Iterator impl */
    };
    static_assert(std::contiguous_iterator<Iterator>);
};

Problem with that is the compiler just says "static assertion failed" and doesn't give me a hint what is actually missing from my implementation to satisfy the concept.

So, is there a better approach to this that I should know about?

modanashar
  • 151
  • 2
  • 9

1 Answers1

2

I don't think there's a way to do that currently with concept. they can only tell if a type meets constraints requirements or not.

source :

How to make a concept fail with a custom error message (C++20)