Question: I'm wondering: is there any possibility to add additional condition to for each?
I'm thinking about something like:

```
int i=0;
for(auto &it : list; i++)
    if(it.ID == 25)
        return i;

```

or 

```
for(auto &it : list, int i=0; i++)
    if(it.ID == 25)
        return i;

```

Answer: 
No it's not possible. You can use the old "normal" `for` loop for that:

```
auto iter = std:begin(list)
for (int i = 0; iter != std::end(list); ++iter, ++i)
{
    auto& it = *iter;

    // ...
}

```
> 
> Mandatory Reference: [Sean Parent's "Seasoning C++" talk](http://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning):
> 
> 
> ### *goal 1: Avoid raw loops*
> 
> 
> 

In cases like these, abstract your algorithm! 

This will come up more often, so it's worth making it generic:

```
#include <algorithm>

template <typename C, typename Pred>
   size_t index_if(C const& c, Pred&& pred)
{
    const auto f(begin(c)), l(end(c));

    auto match = std::find_if(f, l, std::forward<Pred>(pred));
    return (l==match) ? -1 : std::distance(f, match);
}

```

Now you can write your query:

```
int main()
{
    struct X { int ID; };
    const std::vector<X> v { {1},{2},{3},{25},{4},{5},{6},{42} };
    return index_if(v, [](X const& x) { return x.ID == 25; });
}

```

See it **[Live on Coliru](http://coliru.stacked-crooked.com/a/dd8e3bfef0a72a8e)** 

---

PS. You might want a value-based version along with the predicate-based one:

```
template <typename C, typename V/* = typename C::value_type*/>
   size_t index_of(C const& c, V const v)
{
    const auto f(begin(c)), l(end(c));
    auto match = std::find(f, l, v);
    return (l==match) ? -1 : std::distance(f, match);
}

```