CSE 4208: Studio 13

Template Argument Deduction


Today's lecture and studio focus on how template argument types are deduced by the compiler, the type conversions that may occur, and how explicit arguments can be provided to allow specific instantiations (including overcoming ambiguity that may arise from overloading). We will also cover the type transformation templates provided by the type_traits library, and how those can be used for various type conversions including one that is essential to the implementation of the std::move function template that we have discussed previously.


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.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. 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 into that subdirectory the CMakeLists.txt and the source and header files from the your solution for Studio 11, rename files as appropriate (e.g., studio11.cpp to studio13.cpp) and update your CMakeLists.txt accordingly, then confirm that your program builds and runs. As the answer to this exercise, please show the output that your program produced.

  3. Add a template header and source file to your directory, and in them declare and define a function template that is named something other than move and is based on the code that is shown to illustrate the implementation of std::move in today's lecture slides. Make sure that the template header file includes the template source file.

    Update your CMakeLists.txt with the names of those files in the appropriate lines, and update the source file for your main function so that it (1) includes the template header file, and (2) replaces all calls to std::move with calls to the function template you just declared and defined.

    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 all the lines that used to call std::move and that now call your function template instead.

  4. In your program's main function, replace all calls to operator new that are then used to initialize a unique_ptr with calls to std::make_unique instead. Please see the documentation of std::make_unique (which was introduced in C++14) at https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique for syntax and other details.

    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 all the lines that used to call new and that now call std::make_unique instead.

  5. In the source file for your main function, include the <typeinfo> and <type_traits> library header files. In your main function, use the std::remove_reference function template to print out the name of the the type that is pointed to by the unique_ptr. Specifically, after the statement that uses std::make_unique to initialize it, wrap a dereference of the unique_ptr variable within a decltype specification and use that expression to declare a variable of type std::remove_reference<decltype(*up)>::type where up is the name of the unique_ptr variable. Then, use the typeid operator and the name() member function of the type_info object it returns to print out the name of the type to the standard output stream, as in: cout << typeid(v).name() << endl;

    Build and run your program, and as the answer to this exercise please show the type name that was output.

Things to Turn In:

For this studio, please turn in the following: