[odb-users] Object and value at the same time.

Boris Kolpackov boris at codesynthesis.com
Wed Oct 21 13:41:31 EDT 2015


Hi Andreas,

Andreas Pasternak <a.pasternak at mt-robot.com> writes:

> I have a C++ class which I want to use as a value and an object in
> different contexts:
> 
> #pragma db value and object // This command is missing for me
> class Foo {
> int id;
> }
> #pragma db object
> class Bar{
>  Foo m_member;
> }

No, this is not possible. In ODB each C++ type can either be a
value (simple or composite), a persistent object, or a view.

If you really, absolutely must make this work (for example, because
Foo contains a large number of data member that you would like to
avoid repeating), then you could make Foo a class template and
instantiate two (distinct) C++ types out of it:

template <typename T>
class Foo
{
  ...
};

struct ValueTag {};
struct ObjectTag {};

using FooValue = Foo<ValueTag>;
using FooObject = Foo<ObjectTag>;

#pragma db value(FooValue)
#pragma db object(FooObject)

As you can see, this is quite hairy and I would strongly recommend
avoiding this approach if at all possible.

Boris



More information about the odb-users mailing list