flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Can't dereference iterator to array of structures [cpp, gcc, 9.3.0, Linux, master]

Open admo opened this issue 2 years ago • 2 comments

Hi! For given schema

struct S1
{
  f1: int;
  f2: int;
}

struct S2
{
  f1: [S1:16];
}

table Root
{
  f1: S2;
}

root_type Root;

and given C++ code

#include "example_generated.h"

void foo(flatbuffers::Array<S1, 16> const* s)
{
   auto e = *(s->begin());
}

The gcc compiler reports error

$ g++ main.cpp -I../include
In file included from ../include/flatbuffers/array.h:25,
                 from ../include/flatbuffers/flatbuffers.h:24,
                 from example_generated.h:7,
                 from main.cpp:1:
../include/flatbuffers/vector.h: In instantiation of ‘IT flatbuffers::VectorIterator<T, IT, Data, SizeT>::operator*() const [with T = S1; IT = const S1*; Data = const unsigned char*; SizeT = unsigned int]’:
main.cpp:5:25:   required from here
../include/flatbuffers/vector.h:73:56: error: cannot convert ‘flatbuffers::IndirectHelper<S1>::return_type’ {aka ‘S1’} to ‘const S1*’ in return
   73 |   IT operator*() const { return IndirectHelper<T>::Read(data_, 0); }
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
      |                                                        |
      |                                                        flatbuffers::IndirectHelper<S1>::return_type {aka S1}

It causing that such constructions

for (const auto e: *s)...
std::tranform(std::begin(*s), std::end(*s), ... 

do not work.

There is a workaround by using data() and size() member functions:

std::transform(s->data(), s->data()+s->size(), ...

but this is not so convienent like using for-ragne loops or other range managing functions.

The array of primitive types compiles without errors:

#include "example_generated.h"

void foo(flatbuffers::Array<int, 16> const* s)
{
   auto e = *(s->begin());
}

So, I assume that there is a problem with type deduction somewhere in VectorConstIterator or IndirectHelper.

I observed same effects on flatbuffers 2.0.

What do you think? Is it a bug in flatbuffers, or it is expected behavior?

admo avatar Sep 12 '23 11:09 admo

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar May 07 '24 20:05 github-actions[bot]

not-stale

admo avatar May 07 '24 21:05 admo