9. (Move Semantics 101) The objective of this exercise is to get you used to move semantics...
Question:
9. (Move Semantics ‘101’)
The objective of this exercise is to get you used to move semantics and rvalue references.
Answer the following questions:
a) Create a string and move it to another string. Check the contents of the source and target strings before and after the move.
b) Create a vector and move it to another vector. Check the contents of the source and target vectors before and after the move. Compare the time it takes to move a vector compared to the time when using a copy constructor or a copy assignment statement.
c) Consider the following user-defined code to swap two objects:
template < typename T >
void SwapCopyStyle(T&
a, T& b)
{ // Swap a and b in copying way; temporary object needed T tmp(a); // Copy constructor a = b; // Copy all elements from b to a b = tmp; // Copy all elements from tmp to b }
How many temporary copies are created? Now create a function based on move semantics to swap two objects. Compare the relative performance of the two swap functions by timing the swap of two very large vectors. Use std::chrono or your favourite timer. (We discuss std::chrono in Chapter 10.)
10. (Creating Composite Objects)
In this exercise we create a variation and generalised version of the composite class that we introduced in Section 2.4.1 but we now use unique pointers (instead of shared pointers). The related pattern is called Collection Members (POSA, 1996) and it is described as follows:
In this case we aggregate similar objects into one whole; we speak of an organisation and its members. We make no distinction between the members of the collection. The whole provides functionality to clients for iterating over the members and performing operations on them. An example of a collection-members object from everyday life is when we model a house that consists of a number of rooms. Each room has the same interface. Another example is a portfolio of assets, with each C++ asset type having the same interface.
Answer the following questions:
a) Create a class template that consists of a list of instances of a generic type T. These are wrapped in a unique pointer analogously to the class in Section 2.4.1.
b) Create methods to add member to and to remove members from the container. Determine how to use pointers as input arguments. Consider the option of move semantics to add members to the container. What would the advantages be? What are the dangers?
c) Create a member function that iterates over the members of the collection and that performs a command on each one. The command should be a universal function wrapper of the form:
template
using CommandFunctionType = std::function
d) Test the code in parts
a) to
c) by considering a Polyline class that consists of a list of Point instances. Consider commands to execute geometric transformations on the polyline’s members (which are points) such as scaling, rotation and translation. Make the command so generic that it works for a range of functions with the same signature.
This will be a simple example of the Command pattern.
e) Can you find examples of the Collection Members pattern in computational finance?
Choose a representative example and implement it using the same approach as in part d). You need to identify the members of the collection and the corresponding commands.
Step by Step Answer: