<p>I want <code>boost::any_cast&lt;T&gt;</code> to only throw an exception when the type of the <code>any</code> doesn't have an implicit conversion to <code>T</code>. The normal behaviour seems to be to throw an exception if the type of the <code>any</code> is not <code>T</code>, regardless of implicit conversions.</p>

<p>Example:</p>

<pre><code>boost::any a = 1;
boost::any_cast&lt;int&gt;(a); // This succeeds, and rightfully so
boost::any_cast&lt;long&gt;(a); // I don't want this to throw
boost::any_cast&lt;Widget&gt;(a); // I want this to throw
</code></pre>

<p>Could anyone tell me if there's a simple way to get the functionality I want, or better yet give me a good reason for why the existing behaviour is the way it is?</p>

Well you can't do it. The
```
any
```
mechanism works like this:
```
```
struct base {
    virtual ~base() { }
};

template<typename T>
struct concrete_base : base {
    T t;
    concrete_base(T t):t(t) { }
};

struct my_any {
    base * b;

    template<typename T>
    my_any(T t):b(new concrete_base<T>(t)) { }

    template<typename T>
    T any_cast() { 
        concrete_base<T> * t = dynamic_cast< concrete_base<T>* >(b);
        if(!t) throw bad_any_cast();
        return t->t;
    }
};
```
```
I hope it's clear what the above does. There is no way you could do what you are looking for i think. The reason is that there is no information about the type kept that could prove useful here. RTTI doesn't provide it.