Boost logo

Boost Users :

Subject: [Boost-users] Flyweight: wrapping shared_ptr
From: Akim Demaille (akim_at_[hidden])
Date: 2014-10-07 09:06:45


Hi all,

I'd like to add hash-consing on top of my hiearchy-based AST
implementation, which uses shared_ptr (I'm in C++11) to guarantee
value semantics (the pointee is const).

Below is a tiny version that would help me see if using Boost.Flyweight
is adequate.


It works, but it is not transparent, because I have to explicitly
call '.get()' on my flyweights to deference to the pointers. So for
instance my code reads:

  size_t hash() const
  {
    size_t res = 0;
    hash_combine(res, boost::hash_value(op));
    hash_combine(res, l.get()->hash());
    hash_combine(res, r.get()->hash());
    return res;
  }

where I expect to have l->hash(), not l.get()->hash().

I have tried deriving from flyweight to add an operator->, but it
fails horribly.

I realize that it is suboptimal to use flyweight on top of shared_ptr,
as I get two reference counters. Yet this change is less invasive than
migrating my AST hierarchy to using variants as is done in the example
of the documentation.

What would be the right approach?

Thanks a lot.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net