[odb-users] weak_ptr and default pointer type

Boris Kolpackov boris at codesynthesis.com
Fri May 4 05:34:44 EDT 2012


Hi Daniel,

Daniel James <danielpeterjames at gmail.com> writes:

> Is it possible to have the following sort of relationship with the default
> pointer type?
> 
> #pragma db object pointer(*)
> class A {
> ...
> std::weak_ptr<B> b;
> ...
> }
> #pragma db object pointer(*)
> class B {
> ...
> std::shared_ptr<A> a;
> ...
> }
>
> I tried the above and a compile time error points to the following in the
> generated code:
> 
>         // If a compiler error points to the line below, then
>         // it most likely means that a pointer used in a member
>         // cannot be initialized from an object pointer.
>         //
>         o.a = ptr_traits::pointer_type (
>           db->load< obj_traits::object_type > (id));
> 
> It's not explicit in the docs that this is not possible but I suppose it is
> a consequence of std::weak_ptr's constructors?

Chapter 6, "Relationships" in the ODB Manual, first paragraph has this
sentence:

"Any supported smart pointer can be used in a data member as long as it
 can be explicitly constructed from the canonical object pointer. For
 example, we can use weak_ptr if the object pointer is shared_ptr."

The reason why the above won't work is because weak_ptr cannot be
initialized with a raw pointer (only with shared_ptr).

Boris



More information about the odb-users mailing list