Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5.1 A mySum template function with a specialization (3-pt) Write a mySun template function which if passed with a container of int or double (these

image text in transcribed

5.1 A mySum template function with a specialization (3-pt) Write a mySun template function which if passed with a container of int or double (these two only), returns the sum; but when passed with a vector of std::string (this only), returns the total number of characters of all elements. It accepts two type parameters, one for the iterator type of the container, and one for the value type of the container The function is passed with three arguments, first and last of the iterator to mark the range of the container, as well as the initial value of the sum. It returns the summed result Define a type function is_addable, which will be true ONLY when the type argument is either int or double o Note: is_addable is a type predicate (a type function that returns a boolean value). Slide #5 on Lecture 12 shows a type function Array_type that returns an int value (dim) o Here you just need to make is_addable return a bool. The value of the bool is determined by the fact whether the passed I is an int or a double . This is decided by calling another type function which checks if the T is of the same type as an int or a double o Hint: to obtain the value type of the container based on its iterator type Iter, use typename iterator_traits::value_type (see slide#9 on Lecture 12) You can make use of the SameType type function demonstrated in the wikibooks article posted below Lecture 12 In mySum, use static assert to check the following (refer to the end of Lecture 3 for the usage of static_assert, which reports error during compile time) o Only containers of int or double are supported The initial value of the sum must match the value type of the container (e.g. int for vector, and so on) Define a specialization of mySum, which will only be called when a vector of std::string, and an int for initial sum are passed. This version of mySum returns the total number of characters contained in the string vector's elements Write a main() where you test your program with the following, and print out the results of summing these containers: vector v1 = {1, 2, 3, 4}; list double> v2 = {1.1, 2.2, 3.3, 4.4}; vector<:string> v3 = {"Hello", "world", "Yes", "No"}; int sum1 = 1; // the initial sum for v1 - just some value for testing double sum2 = 1.2; // the initial sum for v2 int sum3 = 0; // the initial sum for v3 You should also test your program with the following to allow the compiler to catch errors (but comment them out in your submission) list<:string> v4 = {"Yes", "No"}; float wrong_init = 0.0; cout ::value_type (see slide#9 on Lecture 12) You can make use of the SameType type function demonstrated in the wikibooks article posted below Lecture 12 In mySum, use static assert to check the following (refer to the end of Lecture 3 for the usage of static_assert, which reports error during compile time) o Only containers of int or double are supported The initial value of the sum must match the value type of the container (e.g. int for vector, and so on) Define a specialization of mySum, which will only be called when a vector of std::string, and an int for initial sum are passed. This version of mySum returns the total number of characters contained in the string vector's elements Write a main() where you test your program with the following, and print out the results of summing these containers: vector v1 = {1, 2, 3, 4}; list double> v2 = {1.1, 2.2, 3.3, 4.4}; vector<:string> v3 = {"Hello", "world", "Yes", "No"}; int sum1 = 1; // the initial sum for v1 - just some value for testing double sum2 = 1.2; // the initial sum for v2 int sum3 = 0; // the initial sum for v3 You should also test your program with the following to allow the compiler to catch errors (but comment them out in your submission) list<:string> v4 = {"Yes", "No"}; float wrong_init = 0.0; cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+a) How often will you arrive at work in less than 22 minutes?

Answered: 1 week ago

Question

7. Discuss the implications of a skill-based pay plan for training.

Answered: 1 week ago

Question

4. Make recommendations on steps to take to melt the glass ceiling.

Answered: 1 week ago