Boost logo

Boost Users :

From: Niels Dekker - mail address until 2008-12-31 (nd_mail_address_valid_until_2008-12-31_at_[hidden])
Date: 2008-06-30 14:46:54

Peter Foelsche wrote:
> I suggested some time ago to add a special constructor:
> template<class T, size_t SIZE>
> any(T _ap[SIZE]);
> which should do the same like
> any(T *_p);

Ferdinand Prantl wrote:
> Well, a little shorter as the static_cast version but still a
> "superfluous" cast.
> I could not find anything about it but the thread which I referred in
> the first post to. Do you remember why the constructor has not been
> added to the official version?

I don't know, but it makes sense to me: an array is not just a pointer!
boost::any always stores a /copy/ of its constructor argument or
assignment argument. So /if/ it would support built-in arrays, I think
it should copy their elements internally. Still, it appears technically
possible to add array support to boost::any, e.g., by having it
internally hold a wrapper<ValueType>, instead of a ValueType object (as
holder<ValueType>::held data member):

  template<typename ValueType>
  struct wrapper
    ValueType data;

    explicit wrapper(const ValueType & value)
      : data(value)

The wrapper would need to be specialized for array types, copying the
array elements during construction:

  template<typename ElementType, std::size_t NumberOfElements>
  struct wrapper<ElementType[NumberOfElements]>
    typename remove_const<ElementType>::type data[NumberOfElements];

    explicit wrapper(const ElementType (& value)[NumberOfElements])
      std::copy(value, value + NumberOfElements, data);

The data member holder<ValueType>::held should be declared as
wrapper<ValueType>, and references to "held" should be replaced by
"". Would that solve your problem in an elegant way? If so,
you could consider making it a feature request :-)

Kind regards,

Niels Dekker
Scientific programmer at LKEB, Leiden University Medical Center

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at