[odb-users] How to define odb::section for a member defined in base class

Boris Kolpackov boris at codesynthesis.com
Sat Jan 13 08:03:38 EST 2018

Vladimir Yelnikov <vladimir.yelnikov at gmail.com> writes:

> I would like to declare a derived class demo2 with section tied with
> base member data but ODB compiler can't generate proper code with
> error pointing last pragma:
> error: ')' expected at the end of db pragma section
> [...]
> #pragma db member(demo2::data) type("BLOB")  section(demo2::dataSection)

Quoting the manual (emphasis added):

"The section specifier indicates that a data member of a persistent class
belongs to an object section. The single required argument to this specifier
is the name of the section data member. This specifier can only be used on
*direct data members* of a persistent class."

> Is it possible to make things work?

You can use a virtual data member:

#include <deque>
#include <odb/section.hxx>

struct demo
  typedef std::deque<float> data_type;
  data_type data;

  int dummy;

struct demo2 : public demo

  int id;
  odb::section dataSection;

#pragma db object(demo) definition abstract
#pragma db member(demo::data) transient

#pragma db object(demo2)
#pragma db member(demo2::id) id
#pragma db member(demo2::dataSection) load(lazy)
#pragma db member(demo2::data_) virtual(demo::data_type) access(data) type("BLOB") section(dataSection)

Note that I've added a dummy non-transient data member to demo. If it
only needs to contain data, then you can make the whole demo class a
transient base (i.e., simply don't declare it as persistent).


More information about the odb-users mailing list