i want to be able to use & initialize the Parser class, passing in any list of arguments, like so
1 2 3 4
int a, b, c, d;
Parser parser{a,b,c,d}
parser.Parse();
then i want to be able to iterate over the list of passed arguments in the 'Parse' function. How can i achieve this in the most efficient way possible? Also while keeping the simplistic syntax of only having to write a comma separated list of variables? So by that i mean i'd rather not change the constructor to take a std::vector argument, because then i'd need to initialize it by writing Parser parser{std::vector<int>{a,b,c,d}}
also i think that would mean the vector would store pointers to each variable which seems unnecessary as all i really want is to reference them in the Parse function. Essentially nothing should need to be initialized or copied.
Also as a side question is there a way to ensure the Arguments passed in the variadic template are of a specific type? in this example only ints should be allowed
#include <initializer_list>
#include <vector>
#include <iostream>
struct parser
{
std::vector<int> xs;
explicit parser(std::initializer_list<int> xs): xs(xs) {}
};
int main()
{
parser p { 1, 2, 3, 4, 5 };
for (int x : p.xs) std::cout << x << '\n';
}
You can think of std::initializer_list<T> as an array of T const with extra compiler support.
Also as a side question is there a way to ensure the Arguments passed in the variadic template are of a specific type? in this example only ints should be allowed
Yes, but it is unlikely to be worthwhile. That's mostly because it requires a commitment of effort to write and maintain code that doesn't actually make the user's computer do anything. It's also unlikely to prevent mistakes that the compiler wouldn't catch without help.
That doesn't mean the question is not worth exploring as a learning opportunity. Here are some implementations: