Shane Murphy
Shane Murphy

Reputation: 21

No convenient overload for std::for_each for whole range?

Why is there no overload for std::for_each, or any other std::algorithm functions of similar form like:

template<class Container, class UnaryFunction>
UnaryFunction for_each(Container c, UnaryFunction f);

that would basically just do:

template<class Contianer, class UnaryFunction>
UnaryFunction for_each(Container c, UnaryFunction f){
    return std::for_each(c.begin(), c.end(), f);
}

It seems to me that I am often using these types of functions on the entire range of a container, and it seems inconvenient, especially with long container names, to type:

std::for_each(myLongContainerNameThatIsLong.begin(), myLongContainerNameThatIsLong.end(), f);

Is there any detail I'm missing that prevents this, or some mechanism to get the signature I want?

Upvotes: 2

Views: 105

Answers (1)

cigien
cigien

Reputation: 60208

No, there's no fundamental reason you can't do that, and in fact from C++20, you can do exactly that with std::ranges::for_each:

std::ranges::for_each(myLongContainerNameThatIsLong, f);

Upvotes: 2

Related Questions