
On 3/4/11 11:05 AM, "Stewart, Robert" <Robert.Stewart@sig.com> wrote:
Daniel Larimer wrote:
Stewart, Robert escribió:
Couldn't this functionality be added to Boost.Any itself? That is, add another cast that does dynamic_cast so there's only one library but clients get a choice about how to extract values.
The change is perhaps a bit to fundamental. Here is the "effective difference".
struct any { struct holder_base{}; template<typename T> struct holder_impl<T> : holder_base { T held; }; holder_base* data; }
struct dynamic_any { struct holder_base{}; template<typename T> struct holder_impl<T> : T, virtual holder_base {}; holder_base* data; };
Hmmm. What about just adding the equivalent of dynamic_cast<U *>(&held) and dynamic_cast<U &>(held), in a new cast function template (perhaps through the aid of friendship and a private member function) called dynamic_any_cast, to the existing Boost.Any?
The problem is that you must first cast any::placeholder* to any::holder<U>* before you can access holder<U>::held. Unfortunately, any::holder<Derived> does not inherit from any::holder<Base> so you cannot do a dynamic cast from any::placeholder to any::holder<Base> if any::placeholder is actually an instance of any::holder<Derived>. boost::any can do a static cast because it has verified that the types are the same and thus does not need to worry about "offsets", if the types are different, then you would need a virtual template function to calculate the offset between any two types for the cast. Unfortunately, virtual template methods are not an option.
_____ Rob Stewart robert.stewart@sig.com Software Engineer, Core Software using std::disclaimer; Susquehanna International Group, LLP http://www.sig.com
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost