Boost logo

Boost Users :

From: Patrick Guio (patrick.guio_at_[hidden])
Date: 2001-12-10 11:34:30


On Sun, 9 Dec 2001, assaflavieatwork wrote:

> If all you want to do is output the value inside the 'any' you can
> add a virtual member to the place_holder/holder classes (inside any's
> impl) that will output to a ostream&.

That is exactly my need

> in place_holder:
> virtual ostream& write(ostream&) = 0;

You mean placeholder? I have added this declaration as a pure virtual
function in the queries part.

  class placeholder
  {
    ...
public: // queries
    ...
    virtual ostream& write(ostream&) = 0;
  };

> in holder:
> ostream& write(ostream& o) { o << held; };
>

I have added the declaration in the derived class holder from the
abstract base class placeholder

  template<typename ValueType>
  class holder : public placeholder
  {
    ...
  public: // queries
    ...
    ostream& write(std::ostream& os)
    {
      os << held;
    }
  public: // representation
    ValueType held;
  };

> outside any:
> std::ostream& operator << (std::ostream& out, const any& value) {
> return value.write(out);
> };

I don't understand how this can work since the class any contains a
pointer to an object of class placeholder. This object has a member
function write but not the class any itself?
I have another comment more general about the any class
The declaration
placeholder * content;
in the any class might be public or private depending on the value of the
value of the define BOOST_NO_MEMBER_TEMPLATE_FRIENDS.
Is it safe?

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

    private: // representation

        template<typename ValueType>
        friend ValueType * any_cast(any *);

#else

    public: // representation (public so any_cast can be non-friend)

#endif

        placeholder * content;

    };

>
> But that of course means changing the any.hpp file which of course
> isn't something everyone is willing to do...

Why not including a simple overloading of the << operator?

>
> Anyway, it won't help you if you want to read the any back from an
> istream. You'll have to write some type information to the stream or
> else you'll just interpret everything as a string.
>

I understand that but that is not what I want to do.

Sincerely

Patrick Guio


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