[odb-users] Testing whether lazy_shared_ptr is null

Boris Kolpackov boris at codesynthesis.com
Mon Apr 22 12:17:19 EDT 2019


Brian Coggins <becoggins at hotmail.com> writes:

> Imagine I have two types of object A and B with a unidirectional A to B
> relationship.  If I load an object A containing a lazy_shared_ptr< B >,
> what is the best way to determine whether this lazy_shared_ptr< B >
> represents a relationship to an instance of B (of any kind, transient
> or persistent, loaded or unloaded), or if it is null?

Hm, if you want to check whether the pointer is not NULL (i.e., either
contains an object id or points to a transient object), then that would
simply be:

if (p != nullptr)

If you want to know if the above mentioned object id actually references
something real in the database, then, naturally, the only way to answer
that question is to try to load it.

Also, just to add a point about weak pointers, to correctly test whether
it is NULL you need to call it's lock() function, get the strong pointer,
and then proceed as above.



More information about the odb-users mailing list