With [Boost.Preprocessor] you can easily achieve appropriate code generation for your needs described in the screenshot:
#include <boost/preprocessor/repetition/enum.hpp>
#define ARRAY_ELEMENTS(z, n, arrVar) arrVar[n]
#define EXPAND_ARRAY(N, arrVar) \
BOOST_PP_ENUM(N, ARRAY_ELEMENTS, arrVar)
#define EXPAND_CASE(N, arrVar) \
case N: cout << crawler(EXPAND_ARRAY(N, arrVar)).best_sum << endl; break
int main(int argc, char* argv[]) {
char** input = &argv[1];
--args;
switch(argc) {
EXPAND_CASE(1, input);
EXPAND_CASE(2, input);
EXPAND_CASE(3, input);
}
}
BOOST_PP_REPEAT_FROM_TO
allows to perform further code folding:
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#define ARRAY_ELEMENTS(z, n, arrVar) arrVar[n]
#define EXPAND_ARRAY(N, arrVar) \
BOOST_PP_ENUM(N, ARRAY_ELEMENTS, arrVar)
#define EXPAND_CASE_Z(z, N, arrVar) \
case N: cout << crawler(EXPAND_ARRAY(N, arrVar)).best_sum << endl; break;
// actually each OS has its own limitation for maximal number of arguments, see your
// OS docs for more info. Beware Boost.Preprocessor has its own limits for repeatable
// macros expansions
#define MAX_ARGS 100
int main(int argc, char* argv[]) {
char** input = &argv[1];
--args;
switch(argc) {
BOOST_PP_REPEAT_FROM_TO(1, MAX_ARGS, EXPAND_CASE_Z, input);
}
}
But frankly speaking I doubt variadic template functions are intended to be used this way.