Today's lecture and studio focus on how parameter packs and their expansions can be used to write variadic function and class templates, whose parameterized types (and for function templates whose argument lists) may not be concretely specified until compile-time when the templates are instantiated. Recursive definition of variadic function templates, and application of type patterns and functions when parameter packs are expanded, allow powerful and nuanced functionality to be defined within those variadic templates.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when
you finish them please log into Canvas, select this course in this semester, and
then upload a .txt file containing your answers on the Canvas page
for this studio assignment. Only one submission per team, please, and if
you need to re-submit it the person who originally submitted the studio should
please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
First, ssh into shell.cec.wustl.edu using your WUSTL
Key id and password and then use qlogin to log into one
of the Linux Lab machines and then confirm that the version of g++
there is correct (as you did in Studio 0). Then
cd into your directory for this course this semester, create a new
subdirectory for this studio, and cd into it.
Copy over the CMakeLists.txt, source files, and header files, from your
solution for Studio 14 to your directory for this
studio. Rename any source and header files as appropriate (e.g.,
studio14.h to studio15.h and
studio14.cpp to studio15.cpp, etc.).
Modify the function template that you added in the previous studio so that it
does not insert a short C-style string into the ostream to indicate that
printing happened from the function template, and instead simply inserts the passed
argument of the parameterized type into the ostream and then inserts
a space (" ") into the ostream.
Remove the contents of your program's main function, except for
the statement that returns a descriptively named symbol that has value
0 to indicate success. Above that statement, declare and initialize
variables of the following types: a std::string that is
initialized with a C-style string, a variable of type const char *
that is initialized with a different C-style string, an
object of the class type you declared and defined in the previous
studio that is initialized with a std::string that is
initialized with yet another C-style string, a variable of type
int that is initialized with an integer value, and a
variable of type float that is initialized with a numeric
value that has a fractional portion.
Pass each of the variables you've declared into a separate call to your
your function template that prints each of them out to cout.
Compile and run your program, and as the answer to this exercise, please
show the output that your program produced.
In your template header and source files declare and define a variadic overload
of your function template, with template type parameters consisting of a single
template type parameter followed by a
template parameter pack, a void return type, and a function signature
that takes a reference to an ostream, a const reference to an argument
of the first template parameter type, and a function parameter pack consisting of
const references to the types of the elements of the template parameter pack (i.e.,
wrapping the template parameter pack with const and &
as in const Args&... rest).
That function template should print out the first argument (the const reference to
the instance of the first template parameter type) to the ostream and
then insert a space into the ostream, and then should call itself
recursively with the reference to the ostream followed by the
function parameter pack.
Then modify your main function so that instead of calling the original non-variadic function multiple times, it passes all of the variables it declared into a single call to the variadic template. Compile and run your program and confirm that it produces the same output as in the previous exercise. As the answer to this exercise please show your declaration and definition of the variadic template, and the line in your main function that calls it.
In your template source and header files declare and define another variadic
template with a different name, that has only a template parameter pack in its
template type parameter list, has a void return type, and takes a reference to
an ostream and a function parameter pack consisting of const
references to the types of the elements of the template parameter pack (i.e., again
wrapping the template parameter pack with const and &
as in const Args&... rest).
That function template should use the sizeof... operator to
print out a line to the ostream that says how many elements
are in the function parameter pack that was passed into it, and then should
pass the ostream and the function parameter pack into a call to
the variadic template that you defined in the previous exercise.
In your program's main function replace the call to the variadic template you declared and defined in the previous exercise, with a call to the one you declared and defined in this exercise. Compile and run your program, and as the answer to this exercise please show the output your program produced.
In your template source file, declare a global static unsigned integer variable that is initialized to 0.
In your template header and source file, declare and define another function
template that has a single type parameter, takes a reference to a const variable
of the parameterized type as its only argument, and returns a reference to a const
variable of the parameterized type. That function template should use the plain-old
sizeof operator to determine the size in bytes of the argument that
was passed to it, should add that amount to the global static unsigned integer
variable, and then should return the variable that was passed into it.
Modify the definition of the variadic template you declared and defined in the previous exercise, so that it applies that new function template to each element of the function parameter pack when it calls the variadic template that was declared and defined in the exercise before that. After that call, the the variadic template you declared and defined in the previous exercise should print out a line stating how many bytes were contained within the function parameter pack that was passed into it (based on the value of the global static unsigned integer variable at that point).
Compile and run your program, and as the answer to this exercise please show the output your program produced.
For this studio, please turn in the following: