문제

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?

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top