lundi 14 mai 2018

Template type array of varadic template arguments

I am creating a library for myself that contains functions and macros for things I use often and don't want to constantly retype or copy/paste (sometimes hundreds of lines long). I have a chunk of code in an old project that I have to copy and paste constantly and then change all the variable names to fit my current project. I want to turn the chunk of code into a function in my library.

The big chuck of code formats output into a box so if the output is too long it doesn't ruin any borders I may have or go right to the edge of the console (which bothers me). The user passes the top-left corner of the box and provides the x coordinate of the right edge that the text should not pass. The rest of the arguments are the variables or literals to be outputted. The chunk of code puts all the variables and literals into one big string which is then outputted char by char until the output reaches the x boundary. The output then continues at the x coord of the top-left corner and one line down.

In order for this to work the function must have a varadic template to allow for variable amounts of multiple types of output (similar to cout << "I have " << 4.5 << " cookies and " << 2 << " friends.").

The template is complete and works until I try to create an array of the template type and initialize it.

template<typename ...Inputs>
void textBox(int x, int y, int xBoundary, const Inputs&... things) {
    Inputs arr[sizeof...(things)] = {things...};
}

At this point I get this error: parameter packs not expanded with '...' Note: 'Inputs'

I have used this type of thing before but all the inputs were of the same type for that function so I could easily just do this:

template<typename ...Inputs>
void textBox(int x, int y, int xBoundary, const Inputs&... things) {
    std::string arr[sizeof...(things)] = {things...};
}

That gives no errors unless of course the user tries to pass something other than a string so I know that my error has something to do with making the array of template type. Any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire