Allow CreateVectorOfStrings() to work with any string-type. (#7238)

Any string type that is supported by CreateString(), e.g.
const char* or string_view will now also work.

Signed-off-by: Henner Zeller <hzeller@google.com>
This commit is contained in:
Henner Zeller
2022-04-11 17:02:19 -07:00
committed by GitHub
parent 173ebb6944
commit 7f663b1204
2 changed files with 19 additions and 3 deletions

View File

@@ -716,15 +716,18 @@ class FlatBufferBuilder {
return CreateVector(elems);
}
/// @brief Serialize a `std::vector<std::string>` into a FlatBuffer `vector`.
/// @brief Serialize a `std::vector<StringType>` into a FlatBuffer `vector`.
/// whereas StringType is any type that is accepted by the CreateString()
/// overloads.
/// This is a convenience function for a common case.
/// @param v A const reference to the `std::vector` to serialize into the
/// buffer as a `vector`.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename Alloc = std::allocator<std::string>>
template<typename StringType = std::string,
typename Alloc = std::allocator<std::string>>
Offset<Vector<Offset<String>>> CreateVectorOfStrings(
const std::vector<std::string, Alloc> &v) {
const std::vector<StringType, Alloc> &v) {
return CreateVectorOfStrings(v.cbegin(), v.cend());
}