Big Temp
Big Temp

Reputation: 454

Is there an easy way to test your code against standard's named requirements?

Let's say I wanted to write an iterator for my custom container which uses a custom allocator but still adheres to the C++ standard so that I can be sure it will work with standard algorithms. Is there an easy way to check if all of the above meet the requirements of, say LegacyRandomAccessIterator, Container and Allocator?

I am aware that those requirements are listed here, but let's just say the wording leaves a bit of room for ambiguity at times (LegacyContiguousIterator is literally LegacyRandomAccessIterator hoping that the virtual memory will map 1-to-1 to its underlying physical storage as far as I'm concerned).

Upvotes: 5

Views: 1025

Answers (3)

Nicol Bolas
Nicol Bolas

Reputation: 474376

If C++ had an easy way to test whether a type fit a concept... C++20 wouldn't have needed to create that mechanism. This is after all what C++20 concepts are.

Of course, the C++20 feature introduces standard library concepts which are similar to but not equivalent to the "Legacy" versions. That is, Cpp17RandomAccessIterator is not the same thing as std::random_access_iterator. They're pretty close, but the C++20 testable concept is able to permit things which the C++17 named requirement cannot.

Upvotes: 2

Dietmar Kühl
Dietmar Kühl

Reputation: 154035

With C++20 you can static_assert(...) that concepts are met:

class some_iterator { ... };
static_assert(requires std::random_access_iterator<some_iterator>);

Upvotes: 3

Tarek Dakhran
Tarek Dakhran

Reputation: 2161

There is no unified validation method for STL implementations. Every implementation provides own tests. You can look at examples of Microsoft STL. Adapting tests for your own implementations should not be hard as an interface is the same.

Upvotes: 1

Related Questions