Boost logo

Boost Users :

Subject: Re: [Boost-users] Flyweight: wrapping shared_ptr
From: Akim Demaille (akim_at_[hidden])
Date: 2014-10-09 05:50:58


Hi Joaquin!
Le 8 oct. 2014 à 20:57, Joaquin M Lopez Munoz <joaquin_at_[hidden]> a écrit :

> Akim Demaille <akim <at> lrde.epita.fr> writes:
>
>> /// Conversion to superclass.
>> template <typename U>
>> operator const poly_flyweight<U>&() const
>> {
>> const void* me = this;
>> return *static_cast<const poly_flyweight<U>*>(me);
>> }
>
> Wow, this is totally verboten. The following problems lurk[...]

Thanks for the details, and the real case. Thanks also for prompting
me to dig deeper into the derived_poly_flyweight.

I think it now does what I want, if you're curious:

http://coliru.stacked-crooked.com/a/cacc6107cf22c2e6

I made several changes:

- I want to be able to dynamic_pointer_cast, so I added such
  a function

- but so I also need to support null pointers, so I have added
  support for nullptr and operator bool.

- because of this, I had to write specific hash_value and operator==
  for poly_holder, otherwise the pointer gets dereferenced
  (BTW, I think it's name is confusing, as in Boost.Flyweight
  "holder" denotes something quite different).

- FWIW, your operator== for poly_flyweight was uselessly deep,
  comparing the addresses should suffice (actually, shouldn't
  Boost.Flyweight provide these operators itself? Is there any
  possibility that something else would be wanted? I'm also
  thinking about hashing here).

Here's the kind of things that works:

  Bin e1 = bin('*',
               bin('+', num(1), num(1)),
               bin('+', num(1), num(1)));
  ECHO(e1);
  ECHO(e1->l);
  Exp e2 = e1;
  Exp e3(e1);
  ECHO(e3);
  ECHO(dynamic_pointer_cast<Bin_impl>(e3)->op);
  ECHO(dynamic_pointer_cast<Num_impl>(e3) ? "FAIL" : "pass");

Again, many thanks for your help.


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