This assignment combines topics covered in generic programming, metaprogramming, and functional programming. For pedagogical purposes, please don't make use of type functions or utilities provided by...

This assignment combines topics covered in generic programming, metaprogramming, and functional programming. For pedagogical purposes, please don't make use of type functions or utilities provided by the standard library. That is, these are exercises designed for you to provide your own implementations. 5.1 A mySum template function with a specialization (3-pt)

Write a mySum 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

    • note: is_addableis 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)

    • Here you just need to make is_addable returns a bool. The value of the bool is determined by the fact whether the passed T 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



  • 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)

    • 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, double for list, 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 v2 = {1.1, 2.2, 3.3, 4.4};
vector 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 v4 = {"Yes", "No"};
float wrong_init = 0.0;
cout
cout Example output Sum of v1: 11
Sum of v2: 12.2
Sum of v3: 15
May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here