jeudi 12 octobre 2017

How do I implement a variadic template that reads user input into all variables supplied?

I am currently trying to teach myself variadic templates. However I am having trouble understanding anything past a simple adding template.

Currently I would want a template that would do the following:

  1. Take any number of types
  2. Takes parameters that requires the user to enter them in the following format:

    T value, string descriptor

  3. It then goes through each variable one by one, printing the descriptor before reading the variable

For example the output should look like this:

x (int) //this is the descriptor
//here something is being read into the variable x
y (int) //this is another descriptor
//something else is being read into y
.
.
.

Since its always the same operation, this should be possible. However my best try looked like this

template<typename t,typename... Args>
void generic_reader(t first,string desc,Args... args)
{
    cout<<desc<<endl;
    cin>>first;
    generic_reader(args);
}

Obviously this doesnt work. However I cant think of another way of doing this. Again I have only started to work with variadic templates.

Can someone show me a solution with a detailed explanation?

Aucun commentaire:

Enregistrer un commentaire