Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2002-09-02 11:00:36


David Abrahams wrote:
> I think we decided that the "right" way to spell this construct was
> "extract<T>(x)..."

Cool way.
I understand how to extract by value or by reference:

  int i, *p;
  dynamic_any dynany;

  {
    dynany = 1;

    // create a copy of a value holded by dynany:
    i = extract<int>(dynany);

    // direct access to a value holded by dynany:
    int & ri = extract<int &>(dynany);
    ri = 2;
    const int & rci = extract<const int &>(dynany);
    assert(ri == rci && rci != i);
  }

  {
    dynany = &i;

    // create a copy of a value holded by dynany:
    p = extract<int *>(dynany);

    // direct access to a value holded by dynany:
    int *& rp = extract<int *&>(dynany);
    rp = 0;
    int * const & rcp = extract<int * const &>(dynany);
    assert(rp == rcp && rcp != p);
  }

But how to extract pointers to value holded by dynamic_any? Through pointer
to dynamic_any?

  // continued
  {
    dynany = i;

    // get a pointer to value holded by dynany:
    p = extract<int *>(&dynany);
    *p = 3;
    const int * pc = extract<const int *>(&dynany);
    assert(*p == *pc);
  }

Imagine that someone forgot to type '&' before dynany:
  dynany = 5;
  p = extract<int *>(dynany); // should be &dynany

This problem is not detected until runtime. At runtime std::bad_cast is
thrown because we are trying to extract pointer to int while dynany holds
int itself! I forgot it several times when I wrote tests.
How to resolve this?
First solution is to deprecate returning a copy to a value holded by
dynamic_any. Second solution is to introduce another function, for example,
extract_address:

  p = extract_address<int>(dynany);

I prefered second solution. Your opinions?

--
Best regards,
Alexander Nasonov
e-mail account: alnsn
e-mail server:  mail.ru

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk