[odb-users] Compile Error with Lazy Pointers

Boris Kolpackov boris at codesynthesis.com
Wed Sep 15 09:38:58 EDT 2021


EDDOWES Scott <scott.eddowes at hds.leica-geosystems.com> writes:

> #include <odb/core.hxx>
> #include <odb/lazy-ptr.hxx>
> 
> #pragma db object pointer(std::shared_ptr)
> class foo
> {
> public:
>   foo(int value = 0) : mValue(value) {}
>   ~foo() {}
> 
> #pragma db id
>   int mId;
>   int mValue;
> };
> 
> #pragma db object pointer(std::shared_ptr)
> class bar
> {
> public:
>   bar() {}
>   ~bar() {}
> 
>   void SetFoo(std::shared_ptr<foo>& rpFoo)
>   {
>     mpFoo = rpFoo;
>   }
> 
>   bool GetFooVal(int& rValue)
>   {
>     mpFoo.load();
>     if (mpFoo == nullptr)
>     {
>       return false;
>     }
>     rValue = mpFoo->mValue;
>     return true;
>   }
> 
> #pragma db id
>   int mId;
>   odb::lazy_shared_ptr<foo> mpFoo;
> };

Code that loads the lazy pointer needs to "see" definitions from the
*-odb.hxx file corresponding to the pointed-to object (since load()
effectively calls db.load<T>() underneath).

The simplest way to fix the above code is to make GetFooVal() non-inline
and define it in foobar.cxx. Alternatively, if you want to keep it inline,
you can do something like this:

...

#pragma db object pointer(std::shared_ptr)
class bar
{
  ...

  bool GetFooVal(int& rValue);

  ...

};

#ifndef ODB_COMPILER

#include "foobar-odb.hxx"

inline
bool bar::GetFooVal(int& rValue)
{
  ...
}

#endif



More information about the odb-users mailing list