Boost logo

Boost :

From: Tom Widmer (tom_usenet_at_[hidden])
Date: 2005-01-26 13:18:49


Sérgio Vale e Pace wrote:
> Jonathan Turkanis wrote:
>>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.

Anything is convertible to boost::any, and since you have an operator<<
for boost::any, it will catch any object that doesn't have a better
matched operator<< (i.e. any one that doesn't require a user defined
conversion). If this didn't happen, you'd just get a compiler error when
you tried to create an any<A>, since creation of that requires
instantiation of all virtual functions of holder<A>, including the
ill-formed print member (e.g. no overload matches the call out<<held).

Dispatching to some kind of default call (which might just set failbit
on the stream, or you could make it configurable) is probably the best
way to handle this.

Tom


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