Boost logo

Boost :

From: Sérgio Pace (svpace.forum_at_[hidden])
Date: 2005-01-25 13:46:40


Hi,
  I was playing with the any.hpp library trying to add a generic
inserter to an ostrem. To acomplish that I created a pure virtual
method print in the placeholder:
class placeholder {
...
  virtual void print(std::ostream& out) const = 0;
...
}

implemented it in the template holder

template<typename ValueType>
class holder : public placeholder {
...
  virtual void print(std::ostream& out) const { out<<held; }
...
};

and last but not least I implemented the inserter i the any class:

class any {
...
  friend std::ostream& operator<<(std::ostream& out, const any& data) {
    data.content->print(out);
    return(out);
  };
...
};

this was my first attempt and I expected it to work fine, but to limit
the use of any to classes that implement inserters, but I had a nice
surprise to find out that as long I don´t actually invoke the inserter
on non inserter implementing contained objects everything work fine,
at least in bcc 5.5 and gcc(mingw) 3.4.2. when I do use the inserert
operator for a nom implementing contained object I get a stack
overflow exception, it seens like the inserter operator call the print
method and print invoke the any inserter instead of the contained
object inserter.
  Beeing able to strem out a unknow object is a huge plus in my
current project and I thing it will be a great addition to the
library, but I´m wondering how portable (standard compliant?) is my
solution and why this recursive call happens with non inserter
objects.
  Sorry if my explanation was not very clear or bad english, this is
my first post in this mailing list and english is not my native
language. just mail any need for clarifications.

Sérgio


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