Skip to content

Instantly share code, notes, and snippets.

@victorholt
Last active April 25, 2017 00:36
Show Gist options
  • Save victorholt/d6f4b028765bb77f520c4fb1b52f5bec to your computer and use it in GitHub Desktop.
Save victorholt/d6f4b028765bb77f520c4fb1b52f5bec to your computer and use it in GitHub Desktop.
GenericForEach
#include <iostream>
#include <string>
#include <functional>
#include <vector>
using namespace std;
#define StringArray std::vector<std::string>
#define ForEachMethod(a) std::function<void(const a&)>
#define ForEachTemplate(a, b) template<class a, class b>
#define ForEachString forEach<StringArray, string>
ForEachTemplate(ArrayType, ValueType)
void forEach(const ArrayType& arr, ForEachMethod(ValueType) callback) {
for (auto entry : arr) {
callback(entry);
}
}
int main()
{
StringArray strArr;
strArr.push_back("a");
strArr.push_back("b");
strArr.push_back("c");
strArr.push_back("d");
strArr.push_back("e");
ForEachString(strArr, [](const string& str) {
cout << str << endl;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment