I have a list of functions that return bools. I want to iterate through the list of functions and write a message for each one "Test 1 passed", "Test 2 failed" etc.
My current solution is to create a vector of function pointers, push back each function and then loop through the vector. Code below. Is there a way to avoid the container without repeating the generic message (pass/fail) code for each test (imagine there would be hundreds of tests). It feels as if the vector is unnecessary or that there must be a more elegant solution for this.
typedef bool (*Tests)(); std::vector<Tests> tests; tests.push_back(FASTA_FILE_READER_TEST); tests.push_back(EXACT_MATCH_TEST); for (int i = 0; i < tests.size(); i++) { std::cout << "Test " << i + 1 << (tests[i]() ? " PASSED" : " FAILED") << std::endl; }
reserveif the "wasted" capacity bothers you (it's very rarely a problem).