It often happens that I need to iterate over a list of strings in my C++ code.
In languages like Perl, this is easy:
foreach my $x ("abc", "xyz", "123") {.... }
In the past, this is what I've done in C++
const char* strs[] = { "abc", "xyz", "123" };
for (int i=0; i<sizeof(strs)/sizeof(const char*); i++) {
const char *str = strs[i];
...
If I already have an STL container, I can use BOOST_FOREACH
std::vector<std::string> strs;
BOOST_FOREACH(std::string str, strs) {
...
I've tried to create a macro to combine all these concepts but without success.
I'd like to be able to write code like this:
SPECIAL_STRING_FOREACH(const char* str, {"abc", "xyz", "123"}) {
...
}
Surely someone's cooked this up before.