Pages

Wednesday, 6 January 2021

Chapter 19 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.

Chapter 19 // Exercise 1

Write a template function f() that adds the elements of one vector<T> to elements of another; for example, f(v1, v2) should do v1[i] += v2[i] for each element of v1.


My brain momentarily stopped working when I first read this exercise as I wondered how you check to see if a string is being added to a double. Or how you would define char + float? Or string + size_t? Then I remembered that a templated function with only one template argument can only be of 1 type...brains.

No comments:

Post a Comment