Question

In The C++ Standard Library, it is said

A container might have member functions that provide much better performance.

As SFINAE allows detecting whether such member function exists (here, for instance), it shouldn’t be too complicated to write std::erase such that it calls a container's member function if it exists. Why doesn’t it do that?

Était-ce utile?

La solution

The standard algorithms work with iterator ranges, not containers. Given an iterator, there's no standard way even to tell whether it comes from a container; and certainly no way to access the container if it does.

In order to support this then, as well as bloating the iterator requirements to support the metaprogramming you suggest, you'll also need to bloat each iterator with a pointer/reference to its container. That would be quite a high cost (since many iterator types are usually implemented as a single pointer), for an optimisation that would rarely be needed (since you usually know which type of container you're dealing with).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top