
Jonathan Turkanis wrote:
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.
that´s the odd part it didn´t. as long I don´t use the insert operator i works fine and I don´t understant why it don´t break. see: class A {}; void foo() { A a; int b=0; any x; x = b; cout << x; b = any_cast<int>(x); x = a; // cout << x; // as long as this line is commented everything works fine a = any_cast<A>(x); } on the other hand if I do invoke the insert operator (<<) on a non-OutputStreamable type I get a stack overflow also I don´t understand why.
...You can fix this by dispatching on whether a type is output OutputStreamable, using the metafunction is_default_insertable, here:
great I was looking for some way of doing that. template metaprogramming rules! If I understood correctly you create a last choice overloaded operator<< that return tag and verify if check gets an ostrem or a tag is that correct?
2. You should probably support narrow and wide streams.
good idea
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:
I am following the "BIL" discussion for a while now, and I´m looking forward to put my hands on it
Sérgio
Jonathan