
Hi The following is a partial solution for boost::any operations, including operator==. Adding the operator ValueType& as follows: template <typename ValueType> operator typename ValueType&() const { if (empty() || type() != typeid(ValueType)) boost::throw_exception(bad_any_cast()); return static_cast<any::holder<ValueType> *>(content)->held; } This is different from any_cast in that it returns a reference, instead of a copy. Note also that this will never be used implicitely, you have to call operator ValueType&() explicitely. Operations with values can be added as: template <typename ValueType> bool operator==(const ValueType& rhs) { return rhs == this->operator ValueType&(); } Or template <typename ValueType> any& operator+=(const ValueType& rhs) { this->operator ValueType&() += rhs; return *this; } All operators could be added this way. While this won't break existing code and allows for any object operations, it still has the following drawbacks: 1. No type conversion is performed, (due to template argument deduction). Thus a float can be compared or added to another float, but not to a long, for instance. 2. No operations between any objects can be performed, only between an any and a concret object. 3. The order matters. 0 == any(0) won't compile, while any(0) == 0 will do. 4. It is easy to forget that all operations may throw. If you have any idea of how to overcome (or improve) any of these please let me know. By the way, I'm testing with VC 8.0 and the program compiles and behaves identically calling operator ValueType&() or operator ValueType() with the above declaration. Is that correct? Cheers Jorge

Jorge Lodos wrote:
Hi The following is a partial solution for boost::any operations, including operator==. [ skiped ] This is different from any_cast in that it returns a reference, instead of a copy. Note also that this will never be used implicitely, you have to call operator ValueType&() explicitely.
any_cast already supports reference types.
Operations with values can be added as:
template <typename ValueType> bool operator==(const ValueType& rhs) { return rhs == this->operator ValueType&(); }
Or
template <typename ValueType> any& operator+=(const ValueType& rhs) { this->operator ValueType&() += rhs; return *this; }
There is another way of doing this: typedef mpl::vector< less<typeof(_1 == _2)> , plus<typeof(_1 + _2)> > ops; any<ops> a = 0; any<ops> b = a + a; assert( b == a ); I have a partial solution but I want better multimethods. -- Alexander Nasonov Project Manager http://www.akmosoft.com

Alexander Nasonov wrote:
Jorge Lodos wrote:
Hi The following is a partial solution for boost::any operations, including operator==. [ skiped ] This is different from any_cast in that it returns a reference, instead of a copy. Note also that this will never be used implicitely, you have to call operator ValueType&() explicitely.
any_cast already supports reference types.
Sure, but it returns a copy of the holded object. The described operator returns a reference to the holded object.
There is another way of doing this:
typedef mpl::vector< less<typeof(_1 == _2)> , plus<typeof(_1 + _2)> > ops; any<ops> a = 0; any<ops> b = a + a;
assert( b == a );
I have a partial solution but I want better multimethods.
Nice, thanks for sharing. Cheers Jorge

Jorge Lodos wrote:
Sure, but it returns a copy of the holded object. The described operator returns a reference to the holded object.
No, it returns a reference to held object. any a(0); int& i = any_cast<int&>(a); -- Alexander Nasonov Project Manager http://www.akmosoft.com

What happened to dynamic_any btw? Greetings Franz ----- Original Message ----- From: "Alexander Nasonov" <alnsn@yandex.ru> To: <boost@lists.boost.org> Sent: Tuesday, April 25, 2006 7:13 AM Subject: Re: [boost] boost::any operations
Jorge Lodos wrote:
Sure, but it returns a copy of the holded object. The described operator returns a reference to the holded object.
No, it returns a reference to held object.
any a(0); int& i = any_cast<int&>(a);
-- Alexander Nasonov Project Manager http://www.akmosoft.com _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Dr. Franz Fehringer wrote: > What happened to dynamic_any btw? Two things are missing: 1. Native typeof 2. Spare time to play with typeof emulation or redesign the library -- Alexander Nasonov Project Manager http://www.akmosoft.com

Alexander Nasonov wrote:
Jorge Lodos wrote:
Sure, but it returns a copy of the holded object. The described operator returns a reference to the holded object.
No, it returns a reference to held object.
any a(0); int& i = any_cast<int&>(a);
I stand corrected. Thanks. Cheers Jorge
participants (3)
-
Alexander Nasonov
-
Dr. Franz Fehringer
-
Jorge Lodos