could be just replaced with numtargets = std::min<size_t>(numtargets, targets.size());