I came across an old code that looks like this:
void dothing(bool testBool, const std::string& testString1, const std::string& file, int line, const std::string& defaultString = "") { // do something... } void dothings(bool testBool, const std::string& testString1, const std::string& testString2, const std::string& file, int line, const std::string& defaultString = "") { dothing(testBool, testString1, file, line, defaultString); dothing(testBool, testString2, file, line, defaultString); } void dothings(bool testBool, const std::string& testString1, const std::string& testString2, const std::string& testString3, const std::string& file, int line, const std::string& defaultString = "") { dothings(testBool, testString1, testString2, file, line, defaultString); dothing(testBool, testString3, file, line, defaultString); } void dothings(bool testBool, const std::string& testString1, const std::string& testString2, const std::string& testString3, const std::string& testString4, const std::string& file, int line, const std::string& defaultString = "") { dothings(testBool, testString1, testString2, testString3, file, line, defaultString); dothing(testBool, testString1, file, line, defaultString); }
This is ridiculous, and I'm trying to reorganize it:
void dothings(bool testBool, std::initializer_list<std::string> testStrings, const std::string& file, int line, const std::string& defaultString = "") { for(auto iter = testStrings.begin(); iter != testStrings.end(); ++iter) { dothing(testBool, *iter, file, line, defaultString); } }
The problem is that these functions are used a lot, and I would like to write a macro or template in such a way that all previous functions build a list of string initializations of all test lines and pass them one new function. I want to write something like this:
#define dothings(testBool, (args), file, line) dothings(testBool, {args}, file, line)
I really don't need the default string in these functions, but if there is a way to support it, that would be great.
I have access to the C ++ 11 compiler and ONLY SUPPORT.
I cannot reorder the arguments of these functions.
I saw some interesting posts about variable argument macros, but just didn't press how to apply them to this case.