Skip to content

Create() method of tables with enum vectors takes Vector of underlying type rather than Vector of enum type [C++, HEAD] #5285

Closed
@T045T

Description

It's not possible to create a table that has a vector of enum values without casting those values to the enum's underlying type manually:

test.fbs

enum Color : byte { Red , Green, Blue }
table Colors {
  color_values:[Color];
}
$ flatc -c test.fbs

test.cc

#include "test_generated.h"
#include "flatbuffers/flatbuffers.h"
#include <vector>

int main(int argc, char** argv) {
  flatbuffers::FlatBufferBuilder builder;

  std::vector<Color> colors;
  colors.push_back(Color_Red);
  auto c = builder.CreateVector(colors);
  builder.Finish(CreateColors(builder,c));
}
$ g++ test.cc

test.cc: In function 'int main(int, char**)':
test.cc:11:40: error: could not convert 'c' from 'flatbuffers::Offset<flatbuffers::Vector<Color> >' to 'flatbuffers::Offset<flatbuffers::Vector<signed char> >'
   builder.Finish(CreateColors(builder,c));
                                        ^

This behavior is the same regardless of whether I pass --scoped-enums or not.

What's the reasoning behind generating

inline flatbuffers::Offset<Colors> CreateColors(
    flatbuffers::FlatBufferBuilder &_fbb,
    flatbuffers::Offset<flatbuffers::Vector<int8_t>> color_values = 0)

rather than

inline flatbuffers::Offset<Colors> CreateColors(
    flatbuffers::FlatBufferBuilder &_fbb,
    flatbuffers::Offset<flatbuffers::Vector<Color>> color_values = 0)

?

If compatibility of the generated buffer with other platforms is an issue (which it only is without --scoped-enums), can you consider generating casts to and from the underlying type of the enum to the generated code?

imho, that would be less surprising than the current behavior.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions