Last active
April 25, 2017 00:36
-
-
Save victorholt/d6f4b028765bb77f520c4fb1b52f5bec to your computer and use it in GitHub Desktop.
GenericForEach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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