This is an exercise I am working on alone. The idea is too get familiar with auto, decltype, and type traits. Assuming C++20 isn't a thing yet with concepts, so the implementation goes:
Is my understanding correct? What can be done better?
- Wouldn't you want the static_assert to and the conditions together?
- The for loop could also drop the break statement if you made the terminating condition be i < vt.size() && i < vu.size()
- push_back can incur extra costs if you need to re-allocate the vector multiple times. I would rather reserve max(vt.size(), vu.size()).
- Names are important. What does "f" mean? Give it a meaningful name. The hard part about naming is when you're dealing with templates of potentially unknown types. The more abstract it is, the harder it is to name. What you're doing isn't exactly a dot product since the remaining elements just get tacked on... so maybe something like "multiply_overlapping"? There's no real correct answer, but it's something to think about, and anything is better than "f".
- The standard library generally prefers the use of iterators over explicitly passing objects of <container type>. What this achieves is a better decoupling of the algorithm from the container type itself.
Okay I see some parts that need fixing. I must have misunderstood how to go about adding the multiplied values. I agree with push_back(), should have initialized with the right size. As for the last example, can you show a simple example of how that would look like?