...

/

Solution: Simplifying the Generic Functions

Solution: Simplifying the Generic Functions

The solution to the challenge, "Exercise: Simplifying the Generic Functions".

We'll cover the following...

Solution

Press + to interact
#include <iostream>
#include <vector>
template<typename T>
concept Arithmetic = std::is_arithmetic<T>::value;
template<Arithmetic T>
T sum(const std::vector<T>& cont, T s) {
for (auto x : cont) s += x;
return s;
}
int main() {
std::cout << std::endl;
std::vector vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "sum(vec, 0): " << sum(vec, 0) << std::endl;
std::cout << "sum(vec, 10): " << sum(vec, 10) << std::endl;
}

The ...