
Sérgio Pace wrote:
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:
<snip code>
.. 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 ...
Your code looks fine, and works for me on VC7.1, gcc 3.4.1 (Cygwin) and Intel 8.0 for windows. Two comments, though: 1. Your code breaks boost::any for use with non-OutputStreamable types. You can fix this by dispatching on whether a type is output OutputStreamable, using the metafunction is_default_insertable, here: http://home.comcast.net/~jturkanis/format_lite/libs/format_lite/doc/?path=4.... 2. You should probably support narrow and wide streams.
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.
I'm about to post an announcement for an interface library which can be thought of as a generalization of Boost.Any. Very roughly, while an instance of boost::any can be bound to any object (actually any instance of a value type), an interface reference can be bound to any object of a type which (non-intrusively) implements the interface. Although I haven't implemented operator overloading yet, use will be as follows: using namespace std; interface IStreamable { // pseudocode ostream& operator<<(ostream&); }; int main() { string s = "Hello World!\n"; IStreamable streamable = s; // std::string implements IStreamable cout << streamable; // prints "Hello World!\n"; } Here streamable is an "interface reference"; the library also contains "smart references" which, like interface reference, allow access to the bound object through the dot operator, but in addition manage the lifetime of the bound object, like boost::any.
Sérgio
Jonathan