google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.28k stars 3.25k forks source link

Compilation error while using CreateVectorOfStructs [C++, master] #4392

Closed jitheshtr closed 7 years ago

jitheshtr commented 7 years ago

Hi,

Below is the schema, sample code and error -

$ cat VecOfStructsIssue.fbs
struct TestStruct {
    id :long;
}

table TestTable {
    ids :[TestStruct];
}
#include "VecOfStructsIssue_generated.h"

int main() {
    std::vector<TestStruct> t;
    t.emplace_back(1);
    t.emplace_back(2);
    t.emplace_back(3);

    flatbuffers::FlatBufferBuilder fbb;
    auto table = CreateTestTable(fbb,
                    fbb.CreateVectorOfStructs(
                        t.size(),
                        std::function<void(size_t, TestStruct *)>(
                            [&t](size_t i, TestStruct *e) {
                                *e = TestStruct(t[i].id());
                            }
                        )));
}
$ g++ Test.cc -std=c++11
In file included from VecOfStructsIssue_generated.h:7:0,
                 from Test.cc:1:
/usr/include/flatbuffers/flatbuffers.h: In instantiation of ‘flatbuffers::Offset<flatbuffers::Vector<const T*> > flatbuffers::FlatBufferBuilder::CreateVectorOfStructs(size_t, const std::__7::function<void(long unsigned int, T*)>&) [with T = TestStruct; size_t = long unsigned int]’:
Test.cc:17:26:   required from here
/usr/include/flatbuffers/flatbuffers.h:1254:41: error: invalid conversion from ‘const TestStruct*’ to ‘TestStruct*’ [-fpermissive]
     T* structs = StartVectorOfStructs<T>(vector_size);
                                         ^
make: *** [test] Error 1
$ g++ --version
g++ (GCC) 5.2.0
Copyright (C) 2015 Free Software Foundation, Inc.

Seems to be the issue with return type having const here. Could you please fix?

Thanks, Jithesh

aardappel commented 7 years ago

Thanks, that appears to be introduced by a recent commit by @stewartmiles.

Any reason you use the lambda version btw, instead of just passing the vector as-is?

stewartmiles commented 7 years ago

Looking at it...

jitheshtr commented 7 years ago

Thanks @aardappel & @stewartmiles 👍

Any reason you use the lambda version btw, instead of just passing the vector as-is?

I was converting from protobuf's RepeatedPtrField<T> to flatbuffers [T'] using lambda and hit this issue. Just created a sample using std::vector for this submission.

stewartmiles commented 7 years ago

Sorry about that. It's fixed in f064a6cc60b0978509280002f93dcb9d08a543d8