Bonita Montero
Bonita Montero

Reputation: 3101

How to test if a type exists in a concept?

How can I test if a member type exists in a concept template argument, i.e. for typename Container, test for Container::reverse_iterator ? What's the proper requires-clause ?

Upvotes: 8

Views: 1588

Answers (2)

Chameleon
Chameleon

Reputation: 2137

If you just want the type to exist, follow answer of @StoryTeller above.

If you want the type to exist and fulfill a specific concept, then do the:

template<class Container>
concept has_reverse_iterator
   = std::random_access_iterator<typename Container::reverse_iterator>;

Upvotes: 1

We do this with the aptly named type requirement:

template<class Container>
concept has_reverse_iterator = requires {
    typename Container::reverse_iterator;
};

Upvotes: 14

Related Questions