Transitioning To Modern C++:: An Overview of C++11/14/17 For C++98 Programmers
Transitioning To Modern C++:: An Overview of C++11/14/17 For C++98 Programmers
Each exercise’s basis files are located in a direct sub-folder of the course’s Labs
folder. Solutions are provided in a further sub-folder named Soln.
This is a paper-and-pen exercise. Please do not use your computer until you
have completed the exercise; at that point, feel free to run the source file
through your compiler to see the exact diagnostics produced.
Plus, for each valid ‘auto’ initialization, determine the initializer’s type.
class X
{
public:
X();
explicit X(int);
// ...
};
template<typename T>
void fun(T value, T value2 = T()) {}
int main()
{
int i = 0, j = 1, k = 2;
const double d = 1.2;
const int size = 50;
float f = 2.3;
auto v1 = i; // ?
auto v2(d); // ?
int v3{k}; // ?
int v4{d}; // ?
auto v5{5}; // ?
auto v6 = v1 + v2; // ?
auto v7 = v3 + v5; // ?
X x1(23); // ?
X x2 = 23; // ?
X x3{23}; // ?
X x4 = {23}; // ?
fun(x1); // ?
fun(x1, x3); // ?
fun({x1, x3}); // ?
HINT: If you cannot come up with the standard STL algorithms that provide
a direct path to the solutions, see the file hints.txt, in the solution
folder, for the names of some useful algorithms.
Extra Credit:
Now that you have written several lambdas, write the function object types
yourself that the lambda expressions you wrote automatically created, and
then invoke them explicitly in place of the lambdas your wrote earlier.
For the following timing tests, run each executable at least 3-4 times until
the timer results stabilize (the first few runs will probably take longer as
system caches are getting saturated.)
Given a non-move-enabled class Widget and test program
loadwidgets.cpp,
4. Compile and time again, both with and without the vw.reserve()
call.
What conclusion can you draw about the relative performance benefit of
using reserve on a vector for move-enabled vs. non-move-enabled
types?
print_tuple(t);
}
Examine the code and implement print for the general case variadic
helper class template print_tuple_helper.
Lab #5: What's the right way to design a class to be copyable but not
movable? Or, “Fun with =delete”
Folder: Labs\delete
You are given the source file widget.cpp, containing a skeletal Widget class and
a main function that exercises default construction, copy construction and copy
assignment.
1. Implement the following member functions for Widget, with tracing
messages:
a) default construction
b) copy construction
c) copy assignment
4. Now implement the two move operations for Widget, with tracing
messages. Test to see if they are getting called when appropriate. If they
aren't, that is probably because your compiler is applying the Return Value
Optimization rather than utilizing your move operations. To force a move
construction, add a statement such as the following:
Widget w4(move(w3));
5. Now, let's say you decide you don't want move operations to exist for
Widgets. Remove your implementations of the two move operations, and
instead define both of the operations as =delete in the class definition.
6. Does your program compile now? (It shouldn't. If it does, see me.)
So, why doesn't it compile? How would you "fix" this situation so that no
move operations exist and the program compiles?