Skip to content

Commit

Permalink
fix(c++): add support to std::vector<bool, CustomAlloc> for CreateVector
Browse files Browse the repository at this point in the history
  • Loading branch information
sssooonnnggg committed Aug 21, 2024
1 parent fb9afba commit d2c5e27
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/flatbuffers/flatbuffer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
/// buffer as a `vector`.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T, typename Alloc = std::allocator<T>>
template<typename T, typename Alloc = std::allocator<T>, typename = std::enable_if_t<!std::is_same_v<T, bool>>>
Offset<Vector<T>> CreateVector(const std::vector<T, Alloc> &v) {
return CreateVector(data(v), v.size());
}
Expand All @@ -809,7 +809,8 @@ template<bool Is64Aware = false> class FlatBufferBuilderImpl {
// vector<bool> may be implemented using a bit-set, so we can't access it as
// an array. Instead, read elements manually.
// Background: https://isocpp.org/blog/2012/11/on-vectorbool
Offset<Vector<uint8_t>> CreateVector(const std::vector<bool> &v) {
template<typename T, typename Alloc = std::allocator<T>, typename = std::enable_if_t<std::is_same_v<T, bool>>>
Offset<Vector<uint8_t>> CreateVector(const std::vector<T, Alloc> &v) {
StartVector<uint8_t>(v.size());
for (auto i = v.size(); i > 0;) {
PushElement(static_cast<uint8_t>(v[--i]));
Expand Down

0 comments on commit d2c5e27

Please sign in to comment.