Struct and Table inheritance in generated code?

37 views
Skip to first unread message

arnet...@gmail.com

unread,
May 31, 2017, 6:44:08 AM5/31/17
to FlatBuffers

Hi,

I'm playing around with the reflection API, and often one has to pass a Table or a Struct to the functions there. However, when I have table or struct I know the type of, I have to issue an ugly reinterpret_cast to make the call - obviously this could be solved by having each table and struct in the generated code inherit from their corresponding reflection types, but I'm curious - was there a reason for not doing so (publically - Table is private-inherited)? Or is it simply that noone needed it and I should go open an Issue?

Here's a snippet I'd like to run:

void f(const Struct& s) {
...
}

void f2(const MyStructType& x) {
  f(s);  // <-- what I want
  f(reinterpret_cast<const Struct&>(x));  // <-- what I have to do
}

Why use the reflection API if I know the type? One use case would be a general-purpose logger that uses the reflection description to log walk the table dynamically, but uses the concrete type to I can look up the right schema using some template traits tricks. The reinterpret_cast that has to be done after feels.. dirty ;)

Another use case would be to make overloads for tables and structs - again useful when writing generic code. Then I can have code like:

void f(const Struct& a) { ... }
void f(const Table& a) { ... }

template <typename T>
void x(const T& t) { f(t); }

.. and let the compile choose the right one.

Cheers,
Jacek

Wouter van Oortmerssen

unread,
May 31, 2017, 8:10:53 PM5/31/17
to arnet...@gmail.com, FlatBuffers
Tables inherit from `Table`, so typically shouldn't need a cast.

Structs don't inherit from `Struct`, this is because `Struct` has a member that would influence the size of a derived class if it were included. The reason that we don't use an empty class is because they can have a size, though apparently C++11 now guarantees that they don't (see http://en.cppreference.com/w/cpp/language/ebo), so we could indeed make it a base class.

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

arnet...@gmail.com

unread,
May 31, 2017, 8:37:28 PM5/31/17
to FlatBuffers, arnet...@gmail.com


On Thursday, 1 June 2017 08:10:53 UTC+8, Wouter van Oortmerssen wrote:
Tables inherit from `Table`, so typically shouldn't need a cast.
They do, but the inheritance is private, so it's not useful for this purpose.
 

Structs don't inherit from `Struct`, this is because `Struct` has a member that would influence the size of a derived class if it were included. The reason that we don't use an empty class is because they can have a size, though apparently C++11 now guarantees that they don't (see http://en.cppreference.com/w/cpp/language/ebo), so we could indeed make it a base class.

Ah, I see, thanks!

Cheers,
Jacek

Reply all
Reply to author
Forward
0 new messages