Skip to content

Commit 63db545

Browse files
committed
[NFC] Add a utility to add the elements of a pack to a vector
When we adopt C++17, we'll be able to do this with a fold expression, but this will be useful in the meantime.
1 parent 01e0f37 commit 63db545

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

include/swift/Basic/STLExtras.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,20 @@ erase_if(std::unordered_set<Key, Hash, KeyEqual, Alloc> &c, Pred pred) {
769769
return startingSize - c.size();
770770
}
771771

772+
/// Call \c vector.emplace_back with each of the other arguments
773+
/// to this function, in order. Constructing an intermediate
774+
/// \c std::initializer_list can be inefficient; more problematically,
775+
/// types such as \c std::vector copy out of the \c initializer_list
776+
/// instead of move.
777+
template <class VectorType, class ValueType, class... ValueTypes>
778+
void emplace_back_all(VectorType &vector, ValueType &&value,
779+
ValueTypes &&...values) {
780+
vector.emplace_back(std::forward<ValueType>(value));
781+
emplace_back_all(vector, std::forward<ValueTypes>(values)...);
782+
}
783+
template <class VectorType>
784+
void emplace_back_all(VectorType &vector) {}
785+
772786
} // end namespace swift
773787

774788
#endif // SWIFT_BASIC_INTERLEAVE_H

0 commit comments

Comments
 (0)