Skip to content

Instantly share code, notes, and snippets.

@eiennohito
Created April 13, 2012 15:26
Show Gist options
  • Save eiennohito/2377677 to your computer and use it in GitHub Desktop.
Save eiennohito/2377677 to your computer and use it in GitHub Desktop.
struct IteratorIterator {
template<class T, typename Func>
auto average(Func foo, T collection) -> decltype(foo(*collection.begin())) {
decltype(foo(*collection.begin())) accum = 0;
int size = 0;
for (typename T::const_iterator i = collection.begin(); i != collection.end(); ++i, size++) {
typename T::const_reference ref = *i;
accum += foo(ref);
}
return accum / size; // (size * 1.0)? Не выстрелить бы в ногу при приведении типов
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment